Jump to content
  • Advertisement
Sign in to follow this  
Kevinator

C newbie having trouble dereferencing pointers!

This topic is 4796 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

Hi, GDNETers! I am in the process of learning C (coming from a Java background) and I wrote a small program to increase my understanding of pointers. It works as intended until I want to dereference a pointer. Here is the code:
#include <stdio.h>

int main(int argc, char *argv[]) {
    int const SIZE = 5;
    int a[SIZE], i, *ip;
    for(i = 0; i < SIZE; i++) {
        a = i;
        ip = &a;
        printf("Array element %d's value: %d\nAddress: %p\n", i, a, ip); /* this line works as intended */
        printf("Next element's value: %d\nNext Address: %p\n", *(++ip), ++ip); /* the dereferencing prints out garbage, but the next address part works */
        printf("=====================\n");
    }
    return 0;
}
I saw the exact same syntax used in a similar program and it seemed to work. Thanks a lot for any help. In the meantime I will try to figure out why this isn't working (as I have for quite some time now). [grin]

Share this post


Link to post
Share on other sites
Advertisement
You're using ++ip twice in the same statement. This produces undefined behavior. And if the behavior WAS defined, this would have caused ip to be incremented twice, which is probably not what you want. Never use this operator on the same variable twice in a statement.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kevinator
Hmm, *(ip + 1) is still printing out garbage. The only difference was that gcc didn't give me any warnings this time.
probably because your array a[SIZE] is never intialised and you never instruct your code to reset the memory space to zero's. (as you can see, ip+1 hasnt been intialised even though a aka. ip has )

Share this post


Link to post
Share on other sites
Thanks, I have it working now, except there's one inconsequential thing that isn't working the way I expected. The last iteration through the loop, *(ip + 1) refers to the value in the address immediately following the array, but not in the array. Every time I run the program, this value is 0. Shouldn't it be garbage some of the time?

Share this post


Link to post
Share on other sites
If you are compiling in debug mode, then depending on settings (and on the size of the array in some cases), nearby memory might be zeroed out. But yeah, accessing this memory is undefined; it's not even guaranteed to belong to you (i.e. you could get a segmentation fault or bus error), so just don't do it. :)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!