addreaa of array

This is a discussion on addreaa of array within the C Programming forums, part of the General Programming Boards category; 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 ...

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

    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
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Lose the j.
    Print the address
    Code:
    printf("address is %p\n", (void*)(a + i));
    Last edited by qny; 11-16-2012 at 05:18 AM.

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

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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 07:18 AM.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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
    Douala, Cameroon
    Posts
    187
    would you mind making your code clearer by spacing out the code.????

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,203
    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%.

  8. #8
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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 03:26 PM.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,203
    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%.

  10. #10
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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
    383

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, 02:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM

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