4

I wanted to make sure that I understand the concept of the pointer to an array of n elements, for example: int (*myarr)[10]; //myarr is a pointer to an array that can hold 10 integer

I've tried the following code:

void main(){ int arr[][3] = { { 10, 20, 50 }, { 30, 60, 90 }, { 22, 92, 63 } }; int(*arrptr)[3]; /* a pointer to an array of 3 ints*/ int *ip = arr[0]; arrptr = arr; int index; for (index = 0; index < 9; index++) { printf("[ %d ][ %p ]\n\n", *ip, ip);ip++; } for (index = 0; index < 3; index++) { printf("%x <-> %p,%d\n", *arrptr, arrptr,**arrptr); arrptr++; } } 

and I got that

*[ 10 ][ 001BFA40 ]* [ 20 ][ 001BFA44 ] [ 50 ][ 001BFA48 ] *[ 30 ][ 001BFA4C ]* [ 60 ][ 001BFA50 ] [ 90 ][ 001BFA54 ] *[ 22 ][ 001BFA58 ]* [ 92 ][ 001BFA5C ] [ 63 ][ 001BFA60 ] *1bfa40* <-> *001BFA40*,10 1bfa4c <-> 001BFA4C,30 1bfa58 <-> 001BFA58,22 

As I understand, the arrptr holds the whole address which the array is pointing to. This would mean that the pointer can move freely in these ranges of addresses.

But, why is the address value that holds arrptr equal to the address that holds the value of arr[0]?

In other words: I know it's normal that *arrptr holds the address of arr[0], but the address of arrptr that contains the place where the arrptr will point to is the same where arr[0] ?!

4
  • 1
    You never print out "the address value that holds arrptr", as you never use &arrptr. Please be more precise in your wording, to avoid misunderstandings. Commented Jul 4, 2015 at 12:32
  • %p is not enough to print the address without using the & ? @alk Commented Jul 4, 2015 at 12:37
  • No. Please see my second answer on this. %p is used to print a void-pointer's value. That's all. Commented Jul 4, 2015 at 12:47
  • If Binky (youtube.com/watch?v=6pmWojisM_E) would help? ;-) Commented Jul 4, 2015 at 12:52

4 Answers 4

4

An array is a contiguous storage area. So using your code snippet these expressions

&arr, arr, arr[0], &arr[0], and &arr[0][0] provide the same address of the storage memory occupied by the array. All them point to the first byte of the storage area.

Let's consider for example this statement

printf("%x <-> %p,%d\n", *arrptr, arrptr,**arrptr); 

arrptr is a pointer having type int ( * )[3]; . Dereferencing this pointer *arrptr you will get array of type int[3] that in turn is implicitly converted to a pointer to its first element that is this expression will have type int * and the both arrptr and *arrptr yield the same value. That is the value itself is not changed. It is the type of the expression that is changed.:)

Thus as *arrptr has type int * and it is a pointer (after implicit converiosn of the corresponding one-dimensional array; C does not have references to objects as C++ has) then expression **arrptr yields an object of type int. According to the code snippet logic it is the first element of each row of the two-dimensional array.

Sign up to request clarification or add additional context in comments.

Comments

2

*arrptr is equal (same type, same value) to arrptr[0]. Since you've assigned arrptr = arr that also means *arrptr is equal to *arr.

Incidentally, using %x to print a pointer technically gives undefined behaviour (as %x is for an unsigned integral value). Use %p instead. Similarly, when using %p it is advisable to convert the pointer being printed to (void *).

5 Comments

"advisable"? If not doing so the code runs into undefiend behaviour.
why %p give me the same address as %x? %x gives me the hex number contained in the *arrptr which is equivalent to &arrptr (by using %p) ?@Peter
that what i mean @alk
@YahiaFarghaly: On a 86_64bit machine x give you 32bits of data whereas p gives you 64bits.
@YahiaFarghaly: The is no &arrptr all over the code you show. It's value never gets printed!
1

why the address value that holds arrptr is equal to the address that holds the value of arr[0]

The address of an array always equals the address of its first element.

For any

T a[n]; /* n > 0 */ 

the expression

&a == &a[0] 

is true.


Also please note that in C a "2D" array is just an array of arrays.

1 Comment

@YahiaFarghaly: So then please explicilty show the address value in question you are curious about.
0

Referring the %p and what it prints:

The following code

void * p = 1; printf("%p, %p\n", p, &p); 

prints something like:

0000000000000001, 7fffffff0234585a 

The first number is the value of p and the second number is where the first number is stored, this is the address of p. (As p is a pointer both values are addresses).

4 Comments

and what the difference ?and which one is correct ? @alk
The 1st %p prints the pointer p's value, the 1, the second %p prints the pointer p' s address (as evaluated by applying the & operator to p), the 7fffffff0234585a.
@haccks: Because they are related to two fully different understandings of the problem. And "two fully different understandings of the problem" I consider as two questions, so I provided two answers ... :-)
This one here could have gone as a comment, but would have been too long anyways. @haccks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.