Thread: addreaa of array

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    24

    addreaa of array

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a[20],i,n,*j;
    clrscr();
    printf("Enter 5 elements\n ");
    for(i=0;i<5;i++){
    scanf("%d",&a[i]);
    j=&a[i];
    }
    for(i=0;i<5;i++){
    printf("Elelent %d ",a[i]);
    printf("address is %u\n",j);
    }
    getch();
    }

    i know this code prints only last address,
    please rectify to print each value

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Lose the j.
    Print the address
    Code:
    printf("address is %p\n", (void*)(a + i));
    Last edited by qny; 11-16-2012 at 06:18 AM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Why not &a[i] ?

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by nonoob View Post
    Why not &a[i] ?
    Having a a pointer (or a decayed array) and i an int: &a[i] and a + i are exactly the same thing.
    The only reason to choose either one is stylistic: I prefer the last one.

    Note that, for the printf() conversion specifier "%p", the cast is required regardless of the style you choose.
    Code:
    printf("%p\n", (void*)&a[i]);
    printf("%p\n", (void*)(a + i));
    Also note that using "%u" with addresses is not garanteed to work (it's Undefined Behaviour).
    Last edited by qny; 11-16-2012 at 08:18 AM.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    My comment was directed more to the OP. He used &a[i] without problem in the scanf. So why did he forget about that in the printf?

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    would you mind making your code clearer by spacing out the code.????

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by qny View Post
    Note that, for the printf() conversion specifier "%p", the cast is required regardless of the style you choose.
    That is not true.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by grumpy View Post
    Quote Originally Posted by qny
    Note that, for the printf() conversion specifier "%p", the cast is required regardless of the style you choose.
    That is not true.
    Please see 7.21.6.3 and 7.21.6.1 in the C11 Standard.

    7.21.6.3 says that the printf() function behaves like fprintf() (described in 7.21.6.1).
    7.21.6.1/8 says that the argument relevant to the conversion specifier p "shall be a pointer to void".

    Also 6.2.5/28 says that pointers to void and pointers to any type other than char do not need to share the same representation and alignment: meaning that (on some implementations) a pointer to int and a pointer to void are not interchangeable.
    Last edited by qny; 11-16-2012 at 04:26 PM.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You're assuming that value and representation are the same thing. They are not. %p prints the value of the pointer, not it's representation.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Value and representation are indeed different things. I know that.

    Imagine for the sake of argument that instead of void* and int* we're dealing with int and long: the value 42 is valid for both types, however
    Code:
    printf("%d\n", 42L);
    The value is the same no matter the type however the representations are different. The above code invokes Undefined Behaviour (same as using %p without a void*). On some computers it will print 42; on others it will print 0; on others it will segfault.

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM