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 5 5598
pandapower <pa********@softhome.net> scribbled the following: 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.
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
"pandapower" <pa********@SoftHome.net> wrote in message
news:37**************************@posting.google.c om... 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.
--
Peter pa********@SoftHome.net (pandapower) writes: 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
Joona I Palaste wrote: pandapower <pa********@softhome.net> scribbled the following:
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.
2. An array of char pointers can also be used as a pointer to pointer.
Yes, indeed.
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.
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= \ ar**********@philips.com
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)
On 2 Feb 2004 01:56:12 -0800, pa********@SoftHome.net (pandapower)
wrote: 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) )
--
Horst This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: Mantorok Redgormor |
last post by:
Can someone point out the section in the standard where it says arrays
decay and where elements of a multi dimensional array decay into
pointer to ints too? Like int array; array decays to
pointer...
|
by: Tweaxor |
last post by:
Hey,
I was trying to figure out was it possible in C to pass the values in an array
from one function to another function. Is the possible in C?
ex. y is the array that holds seven values
If...
|
by: Christopher Benson-Manica |
last post by:
Recently, the following definition was posted to comp.lang.c++:
> char **Arr;
1) Is the type of this object "array of 3 (array of two of pointer to
pointer to char)"? If not, what is it?
...
|
by: Luke Wu |
last post by:
Hello,
I'm having some problems understanding 2 dimensional arrays. My
problem relates to the following code:
#include <stdio.h>
#define M 3
#define N 3
|
by: Kobu |
last post by:
I've read the FAQ and several posts on multidimensional arrays and how
their names decay to pointer to arrays (not pointer to pointers).
If this is so, why does the following code fragment...
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 =...
|
by: gcary |
last post by:
I am having trouble figuring out how to declare a pointer to an array
of structures and initializing the pointer with a value. I've looked
at older posts in this group, and tried a solution that...
|
by: Jess |
last post by:
Hi,
If I have an array of pointer like:
char* a = {"a","b","c"};
then it works fine. Since "a" is effectively "a" char**, I tried the
following, which doesn't work:
char** a =...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |