473,505 Members | 16,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

accessing array element as pointer

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.....
Nov 14 '05 #1
5 9278
junky_fellow <ju**********@yahoo.co.in> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
Nov 14 '05 #2

"junky_fellow" <ju**********@yahoo.co.in> wrote in message
news:8c*************************@posting.google.co m...
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
Nov 14 '05 #3
Sean Kenwrick wrote:

"junky_fellow" <ju**********@yahoo.co.in> wrote in message
news:8c*************************@posting.google.co m...
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.

--
pete
Nov 14 '05 #4
Sean Kenwrick wrote:

"junky_fellow" <ju**********@yahoo.co.in> wrote in message
news:8c*************************@posting.google.co m...
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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
In article <news:8c*************************@posting.google.c om>
junky_fellow <ju**********@yahoo.co.in> writes:
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>.
--
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 #6

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

Similar topics

58
10048
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...
6
2716
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
10
4090
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
8
1852
by: Dawn Minnis | last post by:
Hey guys If I have a program (see codeSnippet1) that I compile to be called test.o Then run it as test.o n n 2 3 4 I want the code to be able to strip out the two characters at the start...
10
9113
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
11
4431
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
12
3856
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...
2
2262
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)
16
6759
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
0
7218
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,...
0
7103
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
7307
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
7370
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...
1
7021
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
5614
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,...
0
4701
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...
0
3188
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...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.