Thread: Pointer help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Pointer help


    Hi folks, trying to run the following piece of code on SunOS 5.10 64bit. Makefile flags -g -v -m64 -lsocket -lnlsfeatures=no%conststrings:

    void main()
    {
    int *ptest;
    long itest = 1;

    ptest = &itest;

    printf("CNR itest is :%ld:\n", itest);
    printf("CNR ptest is :%p:\n", *ptest);
    printf("CNR ptest is :%p:\n", ptest);
    printf("CNR itest is :%p:\n", &itest);

    }

    The output is:
    CNR itest is :1:
    CNR ptest is :0:
    CNR ptest is :ffffffff7ffff400:
    CNR itest is :ffffffff7ffff400:

    I can't understand why the ptest pointer shows 0 instead of 1.
    If I change int *ptest; to long *ptest; it works and shows 1
    but I thought a pointer to int would have the same size as a long? pointer and long being 64 bits. I'm a bit rusty I'm afraid.

    Thanks for any help.
    Cheers,
    Chris
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,648
    Rep Power
    1945
    In your second printf(), you have added the parameter to print the pointer address of the value.
    What do you want/expect to be displayed in this output? you don't want to use the same parameter as in the first printf() call (%ld) ?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by MrFujin
    In your second printf(), you have added the parameter to print the pointer address of the value.
    What do you want/expect to be displayed in this output? you don't want to use the same parameter as in the first printf() call (%ld) ?
    I did try %ld but that also shows 0. So I put the %p in
    to see what that displayed - also 0.

    Thanks,
    Chris
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,163
    Rep Power
    2222
    What's the sizeof of an int on your system? Of a long? What's the endian-ness of your system, little-endian or big-endian?

    itest is a long, while ptest points to an int. Let's assume that an int is I bytes in size while a long is L bytes. If I is less than L, then dereferencing ptest only points you to the first I bytes of itest. If your system is little-endian, then the int value that ptest points to would be 1, but if your system is big-endian then the int value would be zero.

    So again, what are the sizeof's of int and long and is your system big- or little-endian?


    Also, please use code tags to post code here.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,163
    Rep Power
    2222
    Your program throws these warnings (MinGW gcc):
    C:TEST>gcc -Wall hobbes1.c
    hobbes1.c:4: warning: return type of `main' is not `int'
    hobbes1.c: In function `main':
    hobbes1.c:8: warning: assignment from incompatible pointer type
    hobbes1.c:12: warning: format argument is not a pointer (arg 2)

    C:TEST>
    Line 12 is: printf("CNR ptest is :%p:\n", *ptest);
    *ptest is an int, not a pointer.

    I've corrected and augmented your program. Now it not only does not throw any warnings, but it also answers my questions about the sizeof of int and long and whether the system is little- or big-endian:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int *ptest;
        long itest = 1;
    
        ptest = (int*)&itest;
    
        printf("sizeof int = %d\nsizeof long = %d\n\n", sizeof(int), sizeof(long));
        
        printf("CNR itest is :%ld:\n", itest);
        printf("CNR ptest is :%d:\n", *ptest);
        printf("CNR ptest is :%p:\n", ptest);
        printf("CNR itest is :%p:\n", &itest);
    
        printf("\nSystem is %s-endian\n", (*((char*)ptest))?"little":"big");
    
        return 0;
    }
    Running it on a Win32 system (XP) I get:
    C:TEST>a
    sizeof int = 4
    sizeof long = 4

    CNR itest is :1:
    CNR ptest is :1:
    CNR ptest is :0022FF78:
    CNR itest is :0022FF78:

    System is little-endian

    C:TEST>
    Now the question is, what do you get when you compile and run it on your system?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by dwise1_aol
    Your program throws these warnings (MinGW gcc):

    Line 12 is: printf("CNR ptest is :%p:\n", *ptest);
    *ptest is an int, not a pointer.

    I've corrected and augmented your program. Now it not only does not throw any warnings, but it also answers my questions about the sizeof of int and long and whether the system is little- or big-endian:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int *ptest;
        long itest = 1;
    
        ptest = (int*)&itest;
    
        printf("sizeof int = %d\nsizeof long = %d\n\n", sizeof(int), sizeof(long));
        
        printf("CNR itest is :%ld:\n", itest);
        printf("CNR ptest is :%d:\n", *ptest);
        printf("CNR ptest is :%p:\n", ptest);
        printf("CNR itest is :%p:\n", &itest);
    
        printf("\nSystem is %s-endian\n", (*((char*)ptest))?"little":"big");
    
        return 0;
    }
    Running it on a Win32 system (XP) I get:

    Now the question is, what do you get when you compile and run it on your system?
    Thanks very much! That explains the problem - it is possible to move the address of a long into a pointer to int - but there might be problems if it is dereferenced.
    The size of an int is 4 and a long 8. The system is big endien.

    Cheers,
    Chris

IMN logo majestic logo threadwatch logo seochat tools logo