Thread: why same address? Need help...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    3

    why same address? Need help...

    Code:
    #include <stdio.h>
    int main()
    {
      int i;
      char a[]="abc";
      printf("%p %p\n",a[1],i); //different address
      i=a[1];
    
      printf("%c\n",a[1]);
      printf("%c\n",i);
      printf("%d\n",i);
      printf("%p %p\n",a[1],i); //same address
    }
    does anybody can explain this? Thx...

  2. #2
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Actually you have to print the address , is it .If you want the print the address of the variables you have to use '&' since you are trying to get the variables address.If you give the pointer there there there is no need for '&'.

    Code:
    #include <stdio.h>
    main()
    {
      int i;
      char a[]="abc";
      printf("%p %p\n",&a[1],&i);  // use &
      i=a[1];
    
      printf("%c\n",a[1]);
      printf("%c\n",i);
      printf("%d\n",i);
      printf("%p %p\n",&a[1],&i);  //use &
    }
    Result is,

    Code:
    0xbfd70d9d 0xbfd70da0
    b
    b
    98
    0xbfd70d9d 0xbfd70da0

    The following is the correct way with out '&'.
    Code:
    int *p;
    printf("%p",p); // No need for & since it is a pointer
    Last edited by karthigayan; 06-03-2010 at 06:47 AM.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    Quote Originally Posted by karthigayan View Post
    Actually you have to print the address , is it .If you want the print the address of the variables you have to use '&' since you are trying to get the variables address.If you give the pointer there there there is no need for '&'.

    Code:
    #include <stdio.h>
    main()
    {
      int i;
      char a[]="abc";
      printf("%p %p\n",&a[1],&i);  // use &
      i=a[1];
    
      printf("%c\n",a[1]);
      printf("%c\n",i);
      printf("%d\n",i);
      printf("%p %p\n",&a[1],&i);  //use &
    }
    Result is,

    Code:
    0xbfd70d9d 0xbfd70da0
    b
    b
    98
    0xbfd70d9d 0xbfd70da0

    The following is the correct way with out '&'.
    Code:
    int *p;
    printf("%p",p); // No need for & since it is a pointer
    I think I know what you mean.
    But at first I did't add & in front of the variable, it also worked. what's that address?

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    I totally understand.
    if i don't add & in front of the variable, it prints the value of this variable.
    and 62 is binary, change it into decimal. it's 98, it's 'b'.
    thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM