473,408 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

pointers and arrays

can anybody explain me the concept of multi dimensional arrays that deals with pointers in detail....

i hav declared a 3 dimesional array like a[2][3][2]={{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};

then
a,*a,**a,***a,a+1,*a+1,**a+1,***a+1...etc what it means?

can any body explain me how to represent arrays in pointer notation for 3 dimensional arrays
( like in one dimensional arrays &a[1] can be writen as (a+1) and a[1] as *(a+1)....)
Oct 18 '06 #1
7 1811
Banfa
9,065 Expert Mod 8TB
Taking this, slightly modified example

int a[2][3][2]={{{2,4},{7,8},{3,4}},{{2,2},{9,3},{3,4}}};

Declares a 3 dimensional array of int that contains a total of 12 integers 2 x 3 x 2

a has type int (*)[3][2]
a[0] has type int (*)[2]
a[0][0] has type int *

taking

int *p = &a[0][0][0];

and treating p as an int array you should find that

p[0] == 2
p[1] == 4
p[2] == 7
p[3] == 8
...
p[8] == 9
...
p[11] == 4

notice that a[1][1][0] is equivilent to p[8]

8 = (((1 * 3) + 1) * 2) + 0

this may not be clear but each step is take the array index used and multiply by the size of the next index. Noteated in algabra as

Declarations

int a[SX][SY][SZ];
int *p = &a[0][0][0];

then

a[X][Y][Z] is equivilent to p[ (((X * SY) + Y) * SZ) + Z ]
Oct 18 '06 #2
Hello friend,
I am a basic learner....i dint understand the words like
.. a has type int (*)[3][2]
a[0] has type int (*)[2]
a[0][0] has type int *
for the above question......
actually what int (*) syntax mean...pls explain the above three statements meanings deeply........
Oct 19 '06 #3
i was asked this proble in an exam.....

main()
{
int a[2][3][2]={{{2,4},{7,8},{3,4}},{{2,2},{9,3},{3,4}}};
printf("%u %u %u %d\n",a,*a,**a,***a);
printf("%u %u %u %d\n",a+1,*a+1,**a+1,***a+1);
}

could u explain the solution for above problem......

Thanks & Regards
Oct 19 '06 #4
Banfa
9,065 Expert Mod 8TB
OK well lates take a step back given these declareations

int x;
int *y;
int **z;

then

x has type int, i.e. the expresion x evaluates to value of type int
y has type int * i.e. the expresion y evaluates to value of type int *, a pointer to an integer
z has type int ** i.e. the expresion z evaluates to value of type int **, a pointer to a pointer to an integer

YOU NEED TO UNDERSTAND THIS PARAGRAPH
Now the type "int (*)[2]" can be read as a pointer to an integer array of size 2, "int (*)[3][2]" is a pointer to a 2 dimensioned integer array with dimension sizes 3 and 2.

So what is the value of a in the question?
well a is an array int a[2][3][2] so the expression a returns the value &a[0] which has type int (*)[3][2], this is a pointer to the first item in the array a[2] (&a[0]).

So what is the value of *a in the question?
well a has type "int (*)[3][2]" from above so *a has type int [3][2], since this is an array it is converted to a pointer by the compiler as &(*a)[0] type "int (*)[2]" which is quite complex but basically it becomes a pointer to the first item in the array a[2][3] (&a[0][0]),

What is the value of **a in the question?
well *a has type "int (*)[2]" from above so **a has type int [2], since this is an array it is converted to a pointer by the compiler as &(**a)[0] type int * which is quite complex but basically it becomes a pointer to the first item in the array a[2][3][2] (&a[0][0][0]),

Note that it is always converted to a pointer to the first entry in the array so ignoring type

a == *a == **a

Finally what is the value of ***a
well **a has type "int *" from above so ***a has type int, this is not an array it is an int value reference by pointer through it's array name. This will be the first entry in the array a[2][3][2], a[0][0][0].


Now on to a+1
now a has type "int (*)[3][2]" from above, so a+1 is this pointer moved on by 1. In C pointer arithmatic garuntees that if you add 1 to a pointer it moves on by the size of the thing pointed to. That is for any type T (int, float, double, struct etc.)

T *pT;

(pT+1) - pT = sizeof(T) = sizeof(*pT)

so a+1 is a moved on by the sizeof(*a) a has type "int (*)[3][2]" therefore sizeof(*a) == sizeof(int *(*)[3][2]) == sizeof(int [3][2]) or the size of a 2 dimensioned array of integers dimension 3 and 2. So where a = &a[0], a+1 = &a[1].

Similarly for *a+1
*a has type "int (*)[2]" so *a+1 is a moved on by sizeof(int [2]), the size of what *a points to. Where *a == &a[0][0], *a+1 == &a[0][1]

And **a+1
**a has type "int *" so **a+1 is a moved on by sizeof(int), the size of what **a points to. Where **a == &a[0][0][0], **a+1 == &a[0][0][1]

But ***a+1
***a has type int, suddenly we are no longer doing pointer arithmatic we are doing integer arithmatic, take the value of the integer ***a and add 1. Since ***a == a[0][0][0] == 2 then ***a+1 == 2+1 == 3
Oct 19 '06 #5
Thankyou BANFA....thanks for your reply.....i am getting new things and i got one more doubt..pls clarify it......

you said....

So what is the value of a in the question?
well a is an array int a[2][3][2] so the expression a returns the value &a[0] which has type int (*)[3][2], this is a pointer to the first item in the array a[2] (&a[0]).

my doubt is....
How the type of a be int (*)[3][2]..and *a of int (*)[2]...so on.
How this is a pointer to the first item in the array a[2] and how a[2] is related to (&a[0])
could you tell me the detailed explanation of the above wt u said.

Thankyou & Regards.
Oct 20 '06 #6
waiting for your reply.....
Oct 21 '06 #7
dtimes6
73
have you ever seen code like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. int a[20];
  3. 5[a] = 5;
  4. printf("%d",a[5]);
  5.  
  6.  
in fact a is address(pointer) to the array, a[5] is just *(a + 5), and this tells 5[a] is just *(5 + a). The two are same in result, but they are different: in a[b] , a is the base address, b is the address to the base address.
Oct 21 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
6
by: Carl-Olof Almbladh | last post by:
Already in the 1st edition of the "White book", Kerigham and Ritchie states the "C is a general purpose language". However, without what is usually called "assumed size arrays" and built-in...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
14
by: code break | last post by:
what is the difference in this pointers decalarition ? int *ptr; and int (*ptr);
6
by: joelperr | last post by:
Hello, I am attempting to separate a two dimensional array into two one-dimensional arrays through a function. The goal of this is that, from the rest of the program, a data file consisting of...
8
by: Piotrek | last post by:
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas...
0
by: David Thompson | last post by:
On Wed, 09 Apr 2008 12:34:43 +0500, arnuld <arnVuld@ippiVmail.com> wrote: I think you've got the idea, but: - I would be careful about using 'equal'. Pointers and arrays are different things,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.