array to pointer decay question !!

P

pandapower

Hi,
I know about the equivalence of pointer and arrays.But my doubt
comes when its for multidimentional arrays.I have read the C faq but
still have some doubts.

Suppose I have a declaration as
1. char array[x][x] or char array[][x]
and it will get decayed to
2. char *array[x]

similarly if I have a declaration as
3. char *argv[]
it can decay to
4. char **argv
Though its at our prerogative the way we use it.


Now from the above cases we see that,
1. 2 dimentional array can get decayed to an array of char pointers.
2. An array of char pointers can also be used as a pointer to pointer.

If I am correct till now, what I mean to know is that though we have
the cases above,a 2 dimentional array can never directly decay to a
pointer to pointer.


Correct me if I am wrong.

regards
pandapower
 
J

Joona I Palaste

pandapower said:
Hi,
I know about the equivalence of pointer and arrays.But my doubt
comes when its for multidimentional arrays.I have read the C faq but
still have some doubts.
Suppose I have a declaration as
1. char array[x][x] or char array[][x]
and it will get decayed to
2. char *array[x]
Yes.

similarly if I have a declaration as
3. char *argv[]
it can decay to
4. char **argv
Though its at our prerogative the way we use it.
Yes.

Now from the above cases we see that,
1. 2 dimentional array can get decayed to an array of char pointers.
2. An array of char pointers can also be used as a pointer to pointer.
If I am correct till now, what I mean to know is that though we have
the cases above,a 2 dimentional array can never directly decay to a
pointer to pointer.

That is true, i.e. it can never directly decay into a pointer to a
pointer. The rule is, more or less:
An array of type <T> can decay into a pointer to type <T>, but only
once. This new, decayed type, can never further decay into anything.
 
P

Peter Nilsson

pandapower said:
Hi,
I know about the equivalence of pointer and arrays.

They are not equivalent! ;)
But my doubt comes when its for multidimentional arrays.
I have read the C faq but still have some doubts.

Try Steve Summit's course notes as additional reading...

http://www.eskimo.com/~scs/cclass/notes/sx10.html
Suppose I have a declaration as
1. char array[x][x] or char array[][x]
and it will get decayed to
2. char *array[x]

If and what an array 'decays to' depends on the context, but 2 is wrong.

char array[2];
char *p = array; /* array decays to &array[0] */

....

char array[2][5];
char (*p)[5] = array; /* array decays to &array[0]
// which is a pointer to an
// array of 5 char
*/

size_t size = sizeof array; /* array doesn't decay */
char (*q)[2][5] = &array; /* array doesn't decay */

The unary & and sizeof operators are exempt, when array is the only operand.
similarly if I have a declaration as
3. char *argv[]
it can decay to
4. char **argv
Though its at our prerogative the way we use it.

Now from the above cases we see that,
1. 2 dimentional array can get decayed to an array of char pointers.


Now you appear to be talking about function parameters, not 'ordinary'
objects. In such cases, arrays don't 'decay' as a such. It's just that there
happen to be two different syntax methods for the _same_ thing...

int main(int argc, char *argv[]);
int main(int argc, char **argv);

void foo(int array[20][50]);
void foo(int array[][50]);
void foo(int (*array)[50]);

void bar(double array[20][50][7]);
void bar(double array[][50][7]);
void bar(double (*array)[50][7]);

C does not pass arrays by value (i.e. a copy of the whole array is not
passed), it only passes pointers to sequences of objects. So, the array
syntax in function parameter declarations are actually pointer declarations.
2. An array of char pointers can also be used as a pointer to pointer.

Vaguely yes. A named object which is an array decays to a pointer.
If I am correct till now, what I mean to know is that though we have
the cases above,a 2 dimentional array can never directly decay to a
pointer to pointer.

Correct. A two dimensional array can only decay to a pointer to an array.
 
M

Martin Dickopp

I know about the equivalence of pointer and arrays.

There is no such equivalence. The relation between pointers and arrays
is that if an array is used in a value context, it is converted to a
pointer to its first element.
Suppose I have a declaration as
1. char array[x][x] or char array[][x]
and it will get decayed to
2. char *array[x]

Yes, if by "decay to" you mean "is converted to in a value context."
similarly if I have a declaration as
3. char *argv[]
it can decay to
4. char **argv

Well, I don't think saying it *can* decay is correct. In a value
context, it is *always* converted.

Also, note the difference between the types of `a' and `b' in these
examples:

void foo (void) { char a [42]; }

void bar (char b []) { }

`a' has type array-of-char, and gets converted to a pointer in a value
context. `b' already has type pointer-to-char, because there's special
rule for parameter declarations. In a parameter declaration (and only
there!), the innermost [] operator is just a fancy way to declare a
pointer.
Now from the above cases we see that,
1. 2 dimentional array can get decayed to an array of char pointers.
2. An array of char pointers can also be used as a pointer to pointer.

If I am correct till now, what I mean to know is that though we have
the cases above,a 2 dimentional array can never directly decay to a
pointer to pointer.

Correct. All that happens is that `array of T' is converted to `pointer
to T' in a value context. This is also true if `T' is itself an array,
so `array of array of U' is converted to `pointer to array of U'.

Martin
 
A

Arjan Kenter

Joona said:
pandapower said:
Hi,
I know about the equivalence of pointer and arrays.But my doubt
comes when its for multidimentional arrays.I have read the C faq but
still have some doubts.

Suppose I have a declaration as
1. char array[x][x] or char array[][x]
and it will get decayed to
2. char *array[x]


Yes.

No, it decays to char (*array)[x], which is very different:
it's a pointer to an array of chars, while your 2nd declaration
is an array of pointers to char.
similarly if I have a declaration as
3. char *argv[]
it can decay to
4. char **argv
Though its at our prerogative the way we use it.

Yes.


Now from the above cases we see that,
1. 2 dimentional array can get decayed to an array of char pointers.

No, it decays to a pointer to array to char.

Yes, indeed.
That is true, i.e. it can never directly decay into a pointer to a
pointer. The rule is, more or less:
An array of type <T> can decay into a pointer to type <T>, but only
once. This new, decayed type, can never further decay into anything.

Unless you repeat the trick:

int x[10][42];
int *y = (x + 1)[2];

x + 1 has type: int (*x)[42] (decayed once)
(x + 1)[2] has type: int * (decayed again)

But of course our wonderful FAQ will make all this much clearer.

--
ir. H.J.H.N. Kenter ^^
Electronic Design & Tools oo ) Philips Research Labs
Building WAY 3.23 =x= \ (e-mail address removed)
Prof. Holstlaan 4 (WAY31) | \ tel. +31 40 27 45334
5656 AA Eindhoven /|__ \ tfx. +31 40 27 44626
The Netherlands (____)_/ http://www.kenter.demon.nl/

Famous last words: Segmentation Fault (core dumped)
 
H

Horst Kraemer

Hi,
I know about the equivalence of pointer and arrays.

There isn't such a thing. The correct formulation would be: If used in
an expression (except in the expressions sizeof array or &array) an
array of <T> decays to (is implicitly converted to) a pointer to <T>
which points to the initial array element.

But my doubt
comes when its for multidimentional arrays.I have read the C faq but
still have some doubts.

Suppose I have a declaration as
1. char array[x][x] or char array[][x]
and it will get decayed to
2. char *array[x]

No. The expression 'array' will decay to a pointer which may be
produced by the declaration

char (*array) [x]

i.e. pointer to array of x chars. Your definition

char *array[x]

declares a array of x pointers to char which is a very different
thing.

char array[2][5]

is by definition an array of 2 (arrays of 5 chars). Read from left to
right. The first, i.e. only the top 'array' decays to 'pointer'. Same
for

char array [2][3][4];

array of 2 (arrays of 3 (arrays of 4 chars))

This decays to

pointer to (array of (3 arrays of 4 chars)), i.e. to

char (*array) [3][4]

and not to

char * array [3][4]

( array of 3 (arrays of 4 pointers to char) )
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top