Thread: i cant understand something in pointers

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    72

    i cant understand something in pointers

    Code:
    #include <stdio.h>
    
    int main(void){
        
        int *a,*b;
        int c=10;
        a = &c;
        printf("%d\n",&(*b));
        getchar();
        
        return 0;
        
    }
    it prints 51, why? what exactly is this number? it's the memory address of what?

    ty

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    It is printing the address that the variable b is located at. If you wanted to print what variable b actually was, your printf statement should appear as follows:
    Code:
    printf("%d\n",*b);
    Also note that you have not even initialized b.

    Also you are passing an (int *) to printf when it is expecting an int. You should be ok because I'm guessing that the machine you are using they are both the same size, but passing unexpected arguments can often result in undefined behavior.
    Last edited by carrotcake1029; 02-12-2010 at 01:20 PM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM