Doubts on 2D array

This is a discussion on Doubts on 2D array within the C Programming forums, part of the General Programming Boards category; Hi All, Following code : Code: int main() { int ar[][5]= {{1,2,3,4,5},{6,7,8,9,10}}; printf("Val : %d\n",ar[1][2]); printf("Val : %d\n",*(ar[1]+2)); printf("*(ar) : ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    Doubts on 2D array

    Hi All,

    Following code :

    Code:
    int main()
    {
    	int ar[][5]= {{1,2,3,4,5},{6,7,8,9,10}};
    	
    	printf("Val       : %d\n",ar[1][2]);
    	printf("Val       : %d\n",*(ar[1]+2));	
    	printf("*(ar)     : %x\n",*(ar));
    	printf("*(*(ar))  : %x\n",*(*(ar)));
    	printf("ar 		  : %x\n",ar);
    	printf("&ar 		  : %x\n",&ar);
    	printf("ar[0]     : %x\n",ar[0]);
    	printf("&ar[0][0] : %x\n",&ar[0][0]);
    	printf("ar[0][0]  : %x\n",ar[0][0]);
    	printf("(ar[0]+1)  : %x\n",(ar[0]+1));
    	printf("(ar[1])  : %x\n",(ar[1]));
    	return 0;
    }
    Code:
    Output:
    Val       : 8
    Val       : 8
    *(ar)     : 6a335340
    *(*(ar))  : 1
    ar                : 6a335340
    &ar               : 6a335340
    ar[0]     : 6a335340
    &ar[0][0] : 6a335340
    ar[0][0]  : 1
    (ar[0]+1)  : 6a335344
    (ar[1])  : 6a335354

    Question:
    1. Why are the following highlighted values the same (for *ar,ar,&ar)? What is the logic behind such assignment wrt the execution perspective?

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Quote Originally Posted by sanddune008
    1. Why are the following highlighted values the same (for *ar,ar,&ar)? What is the logic behind such assignment wrt the execution perspective?
    In other words, the results show that the address of the array is the same in value as the address of its first element, although these addresses differ in type (i.e., they are not merely numeric addresses but pointers). This makes sense since it saves space.

    By the way, to print a pointer with printf, use %p and cast the pointer to void*
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    Quote Originally Posted by laserlight View Post
    (i.e., they are not merely numeric addresses but pointers).
    Nitpick... they behave like pointers (they're not "real" pointers because they're not lvalues). Or is that what you meant by "although these addresses differ in type"?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Quote Originally Posted by Hodor
    Nitpick... they behave like pointers (they're not "real" pointers because they're not lvalues).
    They (or rather the result of the conversion, in the case of ar and *ar) are of pointer type, hence they are pointers. Otherwise, you better be prepared to argue that 123 is not an integer, because although it is of integer type, it is not an lvalue. Likewise, you better be prepared to argue that malloc does not return a pointer, since the return value of a function is not an lvalue.

    Quote Originally Posted by Hodor
    Or is that what you meant by "although these addresses differ in type"?
    The thing is, "address" can refer to a numeric address, without any type information. In the context of C, such addresses can come with type information, i.e., they can be of a pointer type. The object under consideration by this conceptual bundling of a numeric address with type information is known as a pointer, though it can also be called an address, with the bundling of type implied by context.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    Quote Originally Posted by laserlight View Post
    . Likewise, you better be prepared to argue that malloc does not return a pointer, since the return value of a function is not an lvalue.
    Umm, but it does return an lvalue, or at least something that can be used as an lvalue.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    const pointers are not lvalues.
    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, Sunshine, and read this, this, and this before posting again.

  7. #7
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    I should explain what I meant.

    int array[];

    array = &whatever; /* Not ok */

    int *blah = malloc(1024);

    blah = &whatever; /* Ok */

    So, although the array name behaves like a pointer it's not, strictly, a pointer.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Quote Originally Posted by Hodor
    Umm, but it does return an lvalue, or at least something that can be used as an lvalue.
    Are you sure? As far as I can recall, the return value of a function is not an lvalue (as opposed to say, not a modifiable lvalue). In C++ reference return types are possible, so my statement would not apply.

    Quote Originally Posted by Hodor
    So, although the array name behaves like a pointer it's not, strictly, a pointer.
    An array is not a pointer; an array is converted to a pointer to its first element in most contexts, including the context from post #1. My statements were with respect to "the address of the array" and "the address of its first element", not with respect to the array and the array's first element, so you have a strawman argument.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    Quote Originally Posted by laserlight View Post
    Are you sure? As far as I can recall, the return value of a function is not an lvalue (as opposed to say, not a modifiable lvalue). In C++ reference return types are possible, so my statement would not apply.


    An array is not a pointer; an array is converted to a pointer to its first element in most contexts, including the context from post #1. My statements were with respect to "the address of the array" and "the address of its first element", not with respect to the array and the array's first element, so you have a strawman argument.
    I have to make a confession. The strawman from the Wizard of Oz is a great character. Much better than the tinman. Infinitely better than those stupid flying monkeys (or whatever they were), so thank you for the compliment. That little dog, Toto, went onto a successful singing career (linked below) so my hat off to him as well.

    Toto - Africa - YouTube

    Edit: I used to be scared of the wizard, but it turned out that it was only grumpy behind a curtain pulling levers and twisting dials that released smoke and amplified his voice. He was actually quite nice once revealed, so I'm not scared anymore.
    Last edited by Hodor; 04-03-2014 at 06:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. few doubts
    By progmateur in forum C Programming
    Replies: 8
    Last Post: 12-09-2013, 06:15 PM
  2. Doubts concernign reallocation a 2-d array
    By Michal in forum C Programming
    Replies: 3
    Last Post: 10-19-2012, 04:53 PM
  3. My doubts
    By tony2011 in forum C Programming
    Replies: 42
    Last Post: 11-17-2011, 03:46 AM
  4. C++0x - a few doubts
    By Mario F. in forum C++ Programming
    Replies: 33
    Last Post: 09-23-2006, 02:22 PM
  5. ASP,PHP doubts
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-14-2002, 12:17 PM

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