accessing array element as pointer

J

junky_fellow

Consider a 3-dimensional array:
char a[2][3][4] = { "abcd", "efgh", "ijkl" ,
"mnop", "qrst", "uvwx"
} ;

when i print the addresses for a,a[0] and a[0][0] they are same.
but when i access element a+1, a[0]+1 and a[0][0]+1 they are different.
why is it so?


How does the compiler interpret this?
Is this compiler dependent?

thanx in advance.....
 
J

Joona I Palaste

junky_fellow said:
Consider a 3-dimensional array:
char a[2][3][4] = { "abcd", "efgh", "ijkl" ,
"mnop", "qrst", "uvwx"
} ;
when i print the addresses for a,a[0] and a[0][0] they are same.
but when i access element a+1, a[0]+1 and a[0][0]+1 they are different.
why is it so?

Because of different types.
a has type "pointer to char[3][4]"
a[0] has type "pointer to char[4]"
a[0][0] has type "pointer to char"
How does the compiler interpret this?

When you add 1 to each of these, the compiler interprets it as adding
one "element". One "element" is whatever the pointer points at.
With a, one element is one char[3][4], 12 bytes long. With a[0], one
element is one char[4], 4 bytes long. With a[0][0], one element is one
char, 1 byte long.
Is this compiler dependent?

No. It's completely ISO standard.
thanx in advance.....

Yr wlcm.
 
S

Sean Kenwrick

junky_fellow said:
Consider a 3-dimensional array:
char a[2][3][4] = { "abcd", "efgh", "ijkl" ,
"mnop", "qrst", "uvwx"
} ;

when i print the addresses for a,a[0] and a[0][0] they are same.
but when i access element a+1, a[0]+1 and a[0][0]+1 they are different.
why is it so?


How does the compiler interpret this?
Is this compiler dependent?

thanx in advance.....

Someone else has answered your specific question - I just thought I's point
out that your array definition should be a[2][3][5] to allow for the null
terminating byte that the compiler will add to the end of the string
literals "abcd" etc. Otherwise the last one: "uvwx" is going to
overwrite some unknown memory address causing untold terrors..

Sean
 
P

pete

Sean said:
junky_fellow said:
Consider a 3-dimensional array:
char a[2][3][4] = { "abcd", "efgh", "ijkl" ,
"mnop", "qrst", "uvwx"
} ;
- I just thought I's point
out that your array definition should be a[2][3][5]
to allow for the null
terminating byte that the compiler will add to the end of the string
literals "abcd" etc. Otherwise the last one: "uvwx" is going to
overwrite some unknown memory address causing untold terrors..

You're wrong. His code isn't about strings.

There's a special rule that lets you do things like this:
char array[26] = "abcdefghijklmnopqrstuvwxyz";
when you want an array of char, but not a string.
 
C

CBFalconer

Sean said:
junky_fellow said:
Consider a 3-dimensional array:
char a[2][3][4] = { "abcd", "efgh", "ijkl" ,
"mnop", "qrst", "uvwx"
} ;

when i print the addresses for a,a[0] and a[0][0] they are same.
but when i access element a+1, a[0]+1 and a[0][0]+1 they are different.
why is it so?


How does the compiler interpret this?
Is this compiler dependent?

thanx in advance.....

Someone else has answered your specific question - I just thought I's point
out that your array definition should be a[2][3][5] to allow for the null
terminating byte that the compiler will add to the end of the string
literals "abcd" etc. Otherwise the last one: "uvwx" is going to
overwrite some unknown memory address causing untold terrors..

No it isn't (going to overwrite, or even add a '\0'). It the 4
was omitted from the array description it would.
 
C

Chris Torek

Consider a 3-dimensional array:
char a[2][3][4] = { "abcd", "efgh", "ijkl" ,
"mnop", "qrst", "uvwx"
} ;

when i print the addresses for a,a[0] and a[0][0] they are same.
but when i access element a+1, a[0]+1 and a[0][0]+1 they are different.
why is it so?

I have a pictorial explanation for precisely this sort of thing
in <http://web.torek.net/torek/c/pa.html>.
 

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
474,139
Messages
2,570,805
Members
47,352
Latest member
DianeKulik

Latest Threads

Top