Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

why th ewhile loop not incrementing?

loop

  • Please log in to reply
6 replies to this topic

#1 isha_1

isha_1

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 06 September 2009 - 08:26 PM

when iam trying to execute the below piece of code why the loop is not incrementing to 10 when i have given ++ to increment ?please suggest?
#include <stdio.h>

void inc_count(int count_no)
{
  printf("count is before increment  %d \n" ,count_no);

  ++(count_no);
  printf("count is after increment  %d \n" ,count_no);

}

int main()
{
    int count = 0;

    while (count < 10)
	{
      inc_count(count);
	printf("count is %d \n" , count);

    }    

    return (0);
}

Edited by WingedPanther, 08 September 2009 - 08:03 AM.
add code tags (the # button)

  • 0

#2 veda87

veda87

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 06 September 2009 - 08:38 PM

This will work...
#include <stdio.h>

int inc_count(int count_no)
{
        printf("count is before increment %d \n" ,count_no);

        ++count_no;
        printf("count is after increment %d \n" ,count_no);

        return count_no; /*return the incremented value */

}

int main()
{
int count = 0;

while (count < 10)
{
        count = inc_count(count); /* assign the return value to count */
        printf("count is %d \n" , count);

}

return (0);
}
The inc_count function should return the incremented value and should be assign to the count....
  • 0

#3 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 06 September 2009 - 08:41 PM

Please remember to put code inside of [noparse]
and
[/noparse] tags in the future, or use the # button on the top of the Reply window. This makes code easier to read and keeps its spacing.

Anyway, on to the question. This is because you are using a function to increment the value, remember that when you send a value to a function, you are sending a copy of that value, not the location of the value itself, even if you name it the same thing. Thus, it works like this:
void inc_count(int count_no);
int main(void) {
    int count = 0; // get an int's worth of bytes on the stack and assign the value to 0.
    while (count < 10) { // start looping....
        inc_count(count); // run the inc_count function (see below now!)
        printf("count is %d \n", count); // print out the count value.
    }
    return 0;
}
void inc_count(int count_no) { // receive a COPY of the value of "count", and call it "count_no"
    ++count_no; // increment the value of count_no
    printf("count_no is after increment %i \n", count_no); // print this message...
    // count_no falls out of scope here, no return values.
}
The way to resolve this problem is one small change, do this:
void inc_count(int& count_no);
on a C++ compiler. If you're not using C++, do this instead:
void inc_count(int* count_no)
...
// When you call inc_count:
inc_count(&count);
That'll work.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 isha_1

isha_1

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 06 September 2009 - 08:56 PM

Hi Zeke
Thanks 4 ur answer...
Can u please explain how the 2nd piece of ur code is working when i am using pointer variable. Their also i am not using any return type to store the value of increment ?

#include <stdio.h>

void inc_count(int *count_ptr)
{
  printf("count is before increment  %d \n" ,*count_ptr);

  ++(*count_ptr);
  printf("count is after increment  %d \n" ,*count_ptr);

}

int main()
{
    int count = 0;

    while (count < 10)
	{
      inc_count(&count);
	printf("count is %d \n" , count);

    }    

    return (0);
}

Edited by WingedPanther, 08 September 2009 - 08:04 AM.
fix code tags

  • 0

#5 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 06 September 2009 - 10:48 PM

It's because of what pointers are. They allow for direct manipulation and reference of the value instead of receiving a copy of the value. How the computer sees it may be a bit different to how you see it, all values are placed into memory locations, that each have addresses so they can be referred to later on. These memory locations cannot be moved around and given to other processes, they are where they are, BUT they can be changed, and that's what matters. A pointer is basically a value stored in memory that is equal to the address of another location stored in memory, if that makes sense. Run this small program in your computer:
#include <stdio.h>

int main(void)
{
    int* aptr;
    int anum = 1000;
    aptr = &anum;
    printf("The number anum = %i and the pointer aptr dereferenced equals %i\n", anum, *aptr);
    printf("Anum's address is %p and thus, the value of aptr is %p\n\n", (void*) &anum, (void*) aptr);
    printf("How this works is quite simple, aptr is nothing more than 4 bytes of data.\n");
    printf("... at least on 32 bit machines (on 64 bit it's 8 bytes of data).\n");
    printf("These 4 (8) bytes of data \"point\" to a specific location in your RAM, which\n");
    printf("is determined at runtime. You'll notice by running this program more than\n");
    printf("once that the pointer value changes. This is why you can't save pointers\n");
    printf("on the hard disk. Pointers are used frequently in C and C++ programming,\n");
    printf("and are commonly obscured in other \"higher level\" languages. When you give\n");
    printf("a memory address to a function (by using the \"address of\" operator, &), you\n");
    printf("effectively pass that memory address you saw above to the function by value.\n");
    printf("In this way, when that function \"dereferences\" that pointer (by using the\n");
    printf("dereferencing operator, *), you get the actual value in memory, and you can\n");
    printf("thus CHANGE that value in memory without actually having declared it. All\n");
    printf("programs use this fact to utilize \"dynamically allocated memory\", by using\n");
    printf("\"malloc()\" to ask the computer to give them more memory on the heap and return\n");
    printf("a pointer to that memory.");
    return 0;
}
It's nothing more than a small explanation and showing you what is going on, here's an example program:
#include <stdio.h>

void exampleFunc(int *aptr, int notptr)
{
    printf("NOW IN EXAMPLEFUNC:\naptr points to: %p\n", (void*) aptr);
    printf("The address of notptr is NOW: %p... they're different, look!\n", (void*) ¬ptr);
    printf("notptr's value equals: %i\nAnd aptr's dereferenced value equals: %i\n", notptr, *aptr);
    notptr += 10;
    *aptr += 10;
    printf("We added 10 to both values, notptr: %i, *aptr: %i\n", notptr, *aptr);
    printf("but their addresses are the same. ¬ptr: %p, aptr: %p\n", (void*) ¬ptr, (void*) aptr);
    printf("Now exampleFunc ends, notptr and aptr fall out of scope\n\n");
}

int main(void)
{
    /* Let's make three variables, two ints, and one pointer to an int. */
    int* aptr;
    int notptr = 10, ptdto = 10;
    aptr = &ptdto;
    printf("aptr is a pointer pointing to ptdto. Observe what happens when we\n");
    printf("change ptdto without altering aptr.\n*aptr: %i, ptdto: %i\n", *aptr, ptdto);
    ptdto = 20;
    printf("CHANGE!\n");
    printf("*aptr: %i, ptdto: %i\n", *aptr, ptdto);
    printf("Now observe what happens when we call exampleFunc... here, the\n");
    printf("address of notptr is: %p, and aptr points to: %p\n\n", (void*) ¬ptr, (void*) aptr);
    exampleFunc(aptr /*You can also use &ptdto here!*/, notptr);
    printf("We're back in main. and notptr equals: %i, while *aptr: %i\n", notptr, *aptr);
    printf("and ptdto: %i. ptdto changed, but not notptr! Pointers are a\n", ptdto);
    printf("a double-edged sword, their cautious use leads to amazing code, but be\n");
    printf("warned, they can lead to danger! :)\n");

    return 0;
}
Hope that all helps you understand how pointers work better!
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#6 veda87

veda87

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 06 September 2009 - 11:18 PM

That was really a best explanation... Thanks ZekeDragon....

But can you explain me this... Why have you used
printf("address of notptr is: %p, and aptr points to: %p\n\n", (void*) ¬ptr, (void*) aptr);
instead of
printf("address of notptr is: %p, and aptr points to: %p\n\n", ¬ptr, aptr);

when I tried, they both gave me the same address...

Is there any specific reason for that typecast???
  • 0

#7 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 06 September 2009 - 11:31 PM

Yes. On my compiler, when I don't typecast it gives me a warning in that %p expects a void pointer and received an int pointer. So I simply cast it so that %p gets what it expects. :)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download