multidimensional arrays and pointers

J

junky_fellow

Consider a 2-dimensional array,
char arr[3][3] = { 'a','b','c',
'd','e','f',
'g','h','i'
};

what is the difference between,

(arr + 1) and
*(arr + 1)
 
B

bjrnove

what is the difference between,

(arr + 1) and

this gives you the address of the second element in the array. It's the
same as: &(arr[1])
*(arr + 1)

This gives you the insides of the second element in the array. It's the
same as: arr[1]

You could also do like this:
*(*(arr + 1) + 1)

This should return 'e'. This is the same as: arr[1][1]
 
M

Mike Wahler

Consider a 2-dimensional array,
char arr[3][3] = { 'a','b','c',
'd','e','f',
'g','h','i'
};

what is the difference between,

(arr + 1) and
*(arr + 1)


The expression '(arr + 1)' yields the address
of the type 'char[3]' object (an array) whose
first element is located three bytes past the
starting address of the array 'arr'. The
expression's type is 'char(*)[3]' (pointer to
array of three characters).

*(arr + 1) yields the address of the type 'char'
object located three bytes past the starting address
of the array 'arr'. The expression's type is
'char *' (pointer to type 'char').

Thus, both expressions yield the same value (the
address of the character object with the value 'd'
in your example); however the two expressions' types
are different ('char(*)[3]' and 'char*', respectively.
(The actual address value of the two expressions will
vary among implementations, and often between different
executions of the program on the same platform)

I hope I didn't just do your classwork for you. :)

(If I've erred in the above, someone will be sure to
point it out.) :)

More info about arrays and pointers here:
http://web.torek.net/torek/c/pa.html

Other good information from Mr. Torek on C here:
http://web.torek.net/torek/c/

comp.lang.c FAQ here:
http://www.eskimo.com/~scs/C-faq/top.html

-Mike
 
C

claudius

Hi!

Maybe this will shed a bit of light:
http://pweb.netcom.com/~tjensen/ptr/cpoint.htm, it sure did for me, and
it actually was a whole light house I`d say :) It's a very good and
very well written tutorial on pointers from Ted Jensen, it's a must for
every beginner or know-it-all young programmer.

I'd suggest downloading the PDF. Ow, and you can throw your K&R at the
grabage bin after reading that. It will be pretty useless :)
 
M

Malcolm

Consider a 2-dimensional array,
char arr[3][3] = { 'a','b','c',
'd','e','f',
'g','h','i'
};

what is the difference between,

(arr + 1) and
*(arr + 1)
All the introductory books think that 2d arrays should be introduced at the
same time as 1 dimensional arrays.
In fact 2d arrays are an advanced feature of the C language, and once you
move beyond the very basics land you in all sorts of syntactical
complications.

I suggest don't worry about them and just implement all your 2d arrays as 1d
arrays which you access by
array[y * width + x];
 
E

E. Robert Tisdale

Consider a 2-dimensional array,
> cat f0.c
char f(void) {
const
char arr[3][3] = { 'a','b','c',
'd','e','f',
'g','h','i' };
return arr[1][1];
}
> gcc -Wall -std=c99 -pedantic -c f0.c
f0.c: In function `f':
f0.c:3: warning: missing braces around initializer
f0.c:3: warning: (near initialization for `arr[0]')
> cat f1.c
char f(void) {
const
char arr[3][3] = {{'a','b','c'},
{'d','e','f'},
{'g','h','i'}};
return arr[1][1];
}
 
O

Old Wolf

Mike said:
char arr[3][3] = { 'a','b','c',
'd','e','f',
'g','h','i'
};

what is the difference between,

(arr + 1) and
*(arr + 1)


The expression '(arr + 1)' yields the address
of the type 'char[3]' object (an array) whose
first element is located three bytes past the
starting address of the array 'arr'. The
expression's type is 'char(*)[3]' (pointer to
array of three characters).

*(arr + 1) yields the address of the type 'char'
object located three bytes past the starting address
of the array 'arr'. The expression's type is
'char *' (pointer to type 'char').

You had it right the first time..:) The type of *(arr + 1)
is char[3]. It will decay to 'char *' in most circumstances.
Thus, both expressions yield the same value (the
address of the character object with the value 'd'
in your example);

The first version is the address of the object { 'd', 'e', 'f' }
and the second version can decay to the address of the 'd'.

These are different values according to the C standard definition
of 'value' (which includes 'type'). Usually they would have
the same representation, although the DS9000 might do some
sort of base-offset thing.
 
B

Barry Schwarz

Consider a 2-dimensional array,
char arr[3][3] = { 'a','b','c',
'd','e','f',
'g','h','i'
};

what is the difference between,

(arr + 1) and
*(arr + 1)
All the introductory books think that 2d arrays should be introduced at the
same time as 1 dimensional arrays.
In fact 2d arrays are an advanced feature of the C language, and once you
move beyond the very basics land you in all sorts of syntactical
complications.

I suggest don't worry about them and just implement all your 2d arrays as 1d
arrays which you access by
array[y * width + x];
To the OP - please don't.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top