i cant understand something in pointers

This is a discussion on i cant understand something in pointers within the C Programming forums, part of the General Programming Boards category; 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, ...

  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
    398
    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 12: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, 06:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21