473,657 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array to pointer decay question !!

Hi,
I know about the equivalence of pointer and arrays.But my doubt
comes when its for multidimentiona l 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
Nov 14 '05 #1
5 5631
pandapower <pa********@sof thome.net> scribbled the following:
Hi,
I know about the equivalence of pointer and arrays.But my doubt
comes when its for multidimentiona l 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.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
Nov 14 '05 #2
"pandapower " <pa********@Sof tHome.net> wrote in message
news:37******** *************** ***@posting.goo gle.com...
Hi,
I know about the equivalence of pointer and arrays.
They are not equivalent! ;)
But my doubt comes when its for multidimentiona l 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
Nov 14 '05 #3
pa********@Soft Home.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
Nov 14 '05 #4


Joona I Palaste wrote:
pandapower <pa********@sof thome.net> scribbled the following:
Hi,
I know about the equivalence of pointer and arrays.But my doubt
comes when its for multidimentiona l 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**********@ph ilips.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)

Nov 14 '05 #5
On 2 Feb 2004 01:56:12 -0800, pa********@Soft Home.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 multidimentiona l 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

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10120
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 code... TCHAR myArray; DoStuff(myArray);
2
6401
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 to int. Where is this stated in the standard?
8
29046
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 possible how could one pass these seven values in the array to a function that would check the values. I tried return y but it didn't work
9
2723
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? 2) Could the object be passed to functions with the given prototypes? void foo( char **bar ); /* yes? */
9
2309
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
4
2683
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 compiler and run correctly?: #include <stdio.h> int main()
204
12983
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 = {0,1,2,4,9};
12
3872
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
15
636
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 = {"a","b","c"};
0
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8723
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.