Jump to content
  • Advertisement
Sign in to follow this  
xllx_relient_xllx

Incrementing a Pointer in a loop..

This topic is 5455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, I halfway understand pointers but I came across a problem today and it's been very hard to understand why it's not working. Anyway, what I'm trying to do is assign the value of count to the the pointer *nums++ in the for loop, but it does'nt quite do what i expected it to do, which is print 0 1 2 3 4. instead, I get garbage print like this: -33686019 524806 786688 115822160 0 Press any key to continue -------code--------- #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX 5 main() { int *nums; int count; nums = (int*) malloc(5*sizeof(int)); for (count = 0; count < MAX; ++count) *nums++ = count; for (count = 0; count < MAX; ++count) printf("%d\n", *nums++); return 0; } A freind told me it's because nums is actually doing nums+1 on the first loop and it points to nums[1], so nums[0] never gets assigned the value of count? I thought *nums++ would first assign count's value to nums[0] and then increment to point to nums[1] for the next loop. apparently not and "= count" does'nt even get touched in the expression. maybe Im just so slow and did'nt understood what my freind told me so can you please explain it very carefully in details. Thanks in advanced ps: I know it can be solved by doing *(nums+count) but i want to understand why my code is not working.

Share this post


Link to post
Share on other sites
Advertisement
1) As you said, the first int pointed to by *nums is not used.

2) You aren't resetting the pointer to point back to the first element between loops. What you're doing is assigning the elements 1,2,3,4, and 5, skipping zero, then printing 6,7,8,9,10.

I suggest simplifying things by not using ++ and by keeping a copy of the pointer so you can correctly "reset" the nums pointer.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 5

main()
{
int *nums;
int *element; // Points to individual nums
int count;

nums = (int*) malloc(5*sizeof(int));
element = nums; // Point element at the first num

for (count = 0; count < MAX; ++count)
{
*element = count; // Assign first, then increment.
*element++; // Now you aren't skipping 0
}

element = nums; // Reset element to the first num

for (count = 0; count < MAX; ++count)
{
printf("%d\n", *element);
element++;
}

return 0;
}






Edit: I just read your p.s. You could also solve the problem that way. That would also prevent you from losing the location of the first element.

Share this post


Link to post
Share on other sites
Let me also say that this problem would be much more appropriately solved with array indexing. If your goal was to use pointers specifically in this fashion, then I understand, but generally, you would declare your array and access certain elements like so.


nums[count] = count;




[Edited by - pi_equals_3 on October 12, 2004 12:29:57 AM]

Share this post


Link to post
Share on other sites
That's not right. In the first loop:


*nums++ = count;


is equivalent to:


*nums = count;
nums++;


He's used the postfix ++ operator, so it will increment the pointer after the expression is evaluated.

In other words the only problem is that he was not resetting his pointer to the start of the memory he allocated.

cheers
sam

Share this post


Link to post
Share on other sites
Thanks so much guys. Especially pi_equals_3 for taking the time to simplify the problem with an example. And izzo, you nailed it lol. Thanks so much again guys, I really appreciated.

relient

p.s my first post and wow, incredible forum here.

Share this post


Link to post
Share on other sites
Quote:
Original post by izzo
That's not right. In the first loop:


*nums++ = count;


is equivalent to:


*nums = count;
nums++;


He's used the postfix ++ operator, so it will increment the pointer after the expression is evaluated.

In other words the only problem is that he was not resetting his pointer to the start of the memory he allocated.

cheers
sam

Ah, you got me. The postfix operator struck me as suspicious before I had noticed that the pointer wasn't being reset. Both ways will still work, of course. I think the only downside to the original way is that it's slightly less readable, but still very correct!

Share this post


Link to post
Share on other sites
Quote:
Original post by xllx_relient_xllx
Thanks so much guys. Especially pi_equals_3 for taking the time to simplify the problem with an example. And izzo, you nailed it lol. Thanks so much again guys, I really appreciated.

relient

p.s my first post and wow, incredible forum here.


incredible forum indeed :)

Share this post


Link to post
Share on other sites
Quote:
Original post by xllx_relient_xllx
Thanks so much guys. Especially pi_equals_3 for taking the time to simplify the problem with an example. And izzo, you nailed it lol. Thanks so much again guys, I really appreciated.

relient

p.s my first post and wow, incredible forum here.


incredible forum indeed :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
In that original piece of code you posted if you replace *nums++ with (*nums)++ I believe it should do what you are expecting it to do.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
In that original piece of code you posted if you replace *nums++ with (*nums)++ I believe it should do what you are expecting it to do.


Nah, I don't think that's what he wanted to do, since that will increment the value in memory rather than the pointer value itself. You won't step through the array if you do that..

cheers
sam

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!