473,763 Members | 8,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multidimensiona l array = pointer to pointer ?

I've read the FAQ and several posts on multidimensiona l 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()
{
int example[4][4]={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16};

printf("\nBEFOR E DOUBLE DEREFERENCING of example:\n");
printf("\nexamp le[0][0] = %d\n",example[0][0]);
**example = 100; /* double dereferencing 'example' */
printf("\nAFTER DOUBLE DEREFERENCING of example:\n\n\n" );
printf("\nexamp le[0][0] = %d\n",example[0][0]);

return 0;
}

The code ('example') behaves just like a pointer to a pointer. Then
why isn't it a pointer to a pointer?

Nov 14 '05 #1
4 2698
Is this a logical explanation for what I've posted (based on pointer to
array)? ...

1) 'example' points to example[0]

2) *example is example[0]
(and example[0] is another array name, which points to example[0][0])

3) so *(*example) = *(example[0]) = example[0][0]

I see how that works, except for the fact that *example being equal to
example[0], to me, means the physical array example[0], not the just
the name.
Can anyone explain 2) and how the result of *example can be once again
treated as another array name?

With 'ragged' arrays, the above is much more concrete because there is
an intermediate array of pointers to other arrays (so double
dereferencing takes 2 specific paths to the final destination object).

How can double dereferencing take 2 paths with a conventional array?
Some help would be appreciated

Nov 14 '05 #2
In article <11************ **********@c13g 2000cwb.googleg roups.com>,
Kobu <ko********@gma il.com> wrote:
I've read the FAQ and several posts on multidimensiona l arrays and how
their names decay to pointer to arrays (not pointer to pointers).
Yes.
If this is so, why does the following code fragment compile and run
correctly?:
Learn to love The Rule :-) See <http://web.torek.net/torek/c/pa.html>
et seq.; pay attention to the distinction between "object" and "value"
and what happens when you put an object in a value context.

[snippage]int example[4][4]={ /*...*/ };
Here "example" is an object of type "array 4 of array 4 of int".
**example = 100; /* double dereferencing 'example' */


The inner "*example" needs the "value" of "example", so apply The
Rule, obtaining a pointer to its first element. This first element
has type "array 4 of int", so the pointer has type "pointer to
array 4 of int".

The unary "*" operator then follows the pointer, obtaining the
entire "array 4 of int" (example[0][0] through example[0][3]
inclusive). This is an object of type "array 4 of int".

Now we have *(that), so we need the value of that. Once again,
we need the "value" of an array object, so apply The Rule. The
result is a value of type "pointer to int" pointing to the first
element of the entire "array 4 of int". The unary "*" operator
then follows that pointer, obtaining the (single) int to which
it points.

If you look at the figure in <http://web.torek.net/torek/c/pa.html>,
the two steps here are "find the red circle" and then "find the
black circle". Had you written, say:

*(*(example + 2) + 1)

the steps would be: obtain a red circle, move it forward two units
(two "full red circles" worth of distance), then obtain a black
circle within the moved-down red circle, then move *it* -- the
smaller, black circle -- forward one unit.

This is how indexing works in C, and how it is that arrays and
pointers *work* similarly (by obtaining intermediate pointer values
that are not stored anywhere in C's regular object-storage memory).
You can, of course, make your own intermediate pointers and store
them in memory.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #3
Kobu wrote:
I've read the FAQ and several posts on multidimensiona l arrays and
how their names decay to pointer to arrays (not pointer to pointers).
The use of the word "decay" here is common and unfortunate.
It seems to imply that a pointer is somehow inferior to an array name
or a "degraded" array name.
It might be better and more correct to say that,
"There is an implicit conversion of an array name to a pointer."
If this is so,
why does the following code fragment compiler and run correctly?: cat main.c #include <stdio.h>

int main(int argc, char* argv[]) {
int example[4][4]={{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12},
{13, 14, 15, 16}};

printf("\nBEFOR E DOUBLE DEREFERENCING of example:\n");
printf("\nexamp le[0][0] = %d\n",example[0][0]);

**example = 100; // double dereferencing 'example'

printf("\nAFTER DOUBLE DEREFERENCING of example:\n\n\n" );
printf("\nexamp le[0][0] = %d\n",example[0][0]);

return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main
BEFORE DOUBLE DEREFERENCING of example:

example[0][0] = 1

AFTERDOUBLE DEREFERENCING of example:

example[0][0] = 100
The code ('example') behaves just like a pointer to a pointer.
example is an array of four arrays of four int's each.
*example is a reference to array 0 of example (example[0]).
**example is a reference to element 0 of example[0] (example[0][0]).
Then why isn't it a pointer to a pointer?


In general, there is no memory reserved
for an object of type int** named example
nor is any memory reserved for any array of four objects of type int*
to which example points.
example is just the name of an array of four arrays of four int's each.

Nov 14 '05 #4
Kobu wrote:

I've read the FAQ and several posts on multidimensiona l 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()
{
int example[4][4]={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16};

printf("\nBEFOR E DOUBLE DEREFERENCING of example:\n");
printf("\nexamp le[0][0] = %d\n",example[0][0]);

**example = 100; /* double dereferencing 'example' */

printf("\nAFTER DOUBLE DEREFERENCING of example:\n\n\n" );
printf("\nexamp le[0][0] = %d\n",example[0][0]);

return 0;
}

The code ('example') behaves just like a pointer to a pointer. Then
why isn't it a pointer to a pointer?


Because the array 'example' is still in scope. Thus *example is a
pointer to an array of 4 ints. **example points to the first of
those 4 ints.

However if you pass example off to another function, things are
different.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #5

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

Similar topics

2
6962
by: shane | last post by:
Ive searched a fair bit for the answer, but nothing has come up that matches what i want to do. I'm having an issue with passing and assigning pointers to multidimensional arrays. The code: /*.....*/ TCHAR msg func(msg); /*.....*/
5
4504
by: TLOlczyk | last post by:
I have a brain cramp and I need some help. I have a chunk of code below which demonstrates a problem I have with multidimensional arrays. I want to keep it simple but something specific is getting in the way. int a; int b; int **present; int **next;
9
6672
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the function (which was translated from Fortran code), among other heinous and numerous declarations, is this bit: static float bbuff; static int bkey; static int buse;
2
3450
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You can not use the row 0, and the column 0.
2
6596
by: Gerbus | last post by:
Hi there, I'm working in VC++ 2005 Express, and all I want is to have a dynamic multidimensional array of type Here's my code:
2
2583
by: nitinm | last post by:
hi I want to make a program whose requirement are as following: 1) it has to create an NxN matrix after reading input (i.e. N) from a file in the main() itself. 2) it has to send the array as a parameter to a function. condition 1 is not a problem when i use gmalloc for array initialization. but for condition 2, i require that the function that is called by the main(), it has to specify one of the
2
2292
by: ...vagrahb | last post by:
I am having accessing individual rows from a multidimensional array pass to a function as reference CODE: function Declaration int Part_Buffer(char (*buffer),int Low, int High)
0
4512
by: Szabolcs Borsanyi | last post by:
Dear All, there have been several threads on multidimensional arrays and pointers to arrays, there is still something I could not fully understand. (My point here I have raised already, but stayed unanswered) Let's have an array. float A;
9
4502
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize function: x = new int;
0
9564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10002
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
9938
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
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
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
5270
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
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
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.