473,410 Members | 1,875 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,410 software developers and data experts.

multidimensional arrays and pointers

Consider a 2-dimensional array,
char arr[3][3] = { 'a','b','c',
'd','e','f',
'g','h','i'
};

what is the difference between,

(arr + 1) and
*(arr + 1)

Nov 14 '05 #1
7 1465
> what is the difference between,

(arr + 1) and
this gives you the address of the second element in the array. It's the
same as: &(arr[1])
*(arr + 1)


This gives you the insides of the second element in the array. It's the
same as: arr[1]

You could also do like this:
*(*(arr + 1) + 1)

This should return 'e'. This is the same as: arr[1][1]

--
bjrnove

Nov 14 '05 #2
<ju**********@yahoo.co.in> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Consider a 2-dimensional array,
char arr[3][3] = { 'a','b','c',
'd','e','f',
'g','h','i'
};

what is the difference between,

(arr + 1) and
*(arr + 1)

The expression '(arr + 1)' yields the address
of the type 'char[3]' object (an array) whose
first element is located three bytes past the
starting address of the array 'arr'. The
expression's type is 'char(*)[3]' (pointer to
array of three characters).

*(arr + 1) yields the address of the type 'char'
object located three bytes past the starting address
of the array 'arr'. The expression's type is
'char *' (pointer to type 'char').

Thus, both expressions yield the same value (the
address of the character object with the value 'd'
in your example); however the two expressions' types
are different ('char(*)[3]' and 'char*', respectively.
(The actual address value of the two expressions will
vary among implementations, and often between different
executions of the program on the same platform)

I hope I didn't just do your classwork for you. :-)

(If I've erred in the above, someone will be sure to
point it out.) :-)

More info about arrays and pointers here:
http://web.torek.net/torek/c/pa.html

Other good information from Mr. Torek on C here:
http://web.torek.net/torek/c/

comp.lang.c FAQ here:
http://www.eskimo.com/~scs/C-faq/top.html

-Mike
Nov 14 '05 #3
Hi!

Maybe this will shed a bit of light:
http://pweb.netcom.com/~tjensen/ptr/cpoint.htm, it sure did for me, and
it actually was a whole light house I`d say :) It's a very good and
very well written tutorial on pointers from Ted Jensen, it's a must for
every beginner or know-it-all young programmer.

I'd suggest downloading the PDF. Ow, and you can throw your K&R at the
grabage bin after reading that. It will be pretty useless :)

Nov 14 '05 #4

<ju**********@yahoo.co.in> wrote
Consider a 2-dimensional array,
char arr[3][3] = { 'a','b','c',
'd','e','f',
'g','h','i'
};

what is the difference between,

(arr + 1) and
*(arr + 1)

All the introductory books think that 2d arrays should be introduced at the
same time as 1 dimensional arrays.
In fact 2d arrays are an advanced feature of the C language, and once you
move beyond the very basics land you in all sorts of syntactical
complications.

I suggest don't worry about them and just implement all your 2d arrays as 1d
arrays which you access by
array[y * width + x];
Nov 14 '05 #5
ju**********@yahoo.co.in wrote:
Consider a 2-dimensional array, cat f0.c char f(void) {
const
char arr[3][3] = { 'a','b','c',
'd','e','f',
'g','h','i' };
return arr[1][1];
}
gcc -Wall -std=c99 -pedantic -c f0.c f0.c: In function `f':
f0.c:3: warning: missing braces around initializer
f0.c:3: warning: (near initialization for `arr[0]') cat f1.c char f(void) {
const
char arr[3][3] = {{'a','b','c'},
{'d','e','f'},
{'g','h','i'}};
return arr[1][1];
}
gcc -Wall -std=c99 -pedantic -c f1.c

Nov 14 '05 #6
Mike Wahler wrote:
<ju**********@yahoo.co.in> wrote:
char arr[3][3] = { 'a','b','c',
'd','e','f',
'g','h','i'
};

what is the difference between,

(arr + 1) and
*(arr + 1)

The expression '(arr + 1)' yields the address
of the type 'char[3]' object (an array) whose
first element is located three bytes past the
starting address of the array 'arr'. The
expression's type is 'char(*)[3]' (pointer to
array of three characters).

*(arr + 1) yields the address of the type 'char'
object located three bytes past the starting address
of the array 'arr'. The expression's type is
'char *' (pointer to type 'char').


You had it right the first time..:) The type of *(arr + 1)
is char[3]. It will decay to 'char *' in most circumstances.
Thus, both expressions yield the same value (the
address of the character object with the value 'd'
in your example);


The first version is the address of the object { 'd', 'e', 'f' }
and the second version can decay to the address of the 'd'.

These are different values according to the C standard definition
of 'value' (which includes 'type'). Usually they would have
the same representation, although the DS9000 might do some
sort of base-offset thing.

Nov 14 '05 #7
On Tue, 31 May 2005 22:06:50 +0000 (UTC), "Malcolm"
<re*******@btinternet.com> wrote:

<ju**********@yahoo.co.in> wrote
Consider a 2-dimensional array,
char arr[3][3] = { 'a','b','c',
'd','e','f',
'g','h','i'
};

what is the difference between,

(arr + 1) and
*(arr + 1)

All the introductory books think that 2d arrays should be introduced at the
same time as 1 dimensional arrays.
In fact 2d arrays are an advanced feature of the C language, and once you
move beyond the very basics land you in all sorts of syntactical
complications.

I suggest don't worry about them and just implement all your 2d arrays as 1d
arrays which you access by
array[y * width + x];

To the OP - please don't.
<<Remove the del for email>>
Nov 14 '05 #8

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

Similar topics

3
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...
2
by: Terry | last post by:
Hi, can someone plz tell me how multidimensional arrays (like a 2-D array) are stored in memory? Are they like single dimensional arrays? Stored sequentially in one "row", so to say? Thanks ...
9
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...
4
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...
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
6
by: c19h28o2 | last post by:
Hi, I'm learning to use pointers with multidemensional arrays so please bear with me! Here is the problem which is from c primer plus Ch10 Programming excercise 1 Modify the rain program...
8
by: ajayicon | last post by:
Hi all, I am not an expert in C..more of an algorithm developer. One of my algorithm requires use of multidimensional struct. for example something like: struct cell{ int row; ...
2
by: oopsatwork | last post by:
Ok...so, I have been outside of the C world for a _very_ long time...but not so long as to remember how to do multidimensional arrays. So, let me state that I know how to malloc pointers to...
0
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...
9
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...
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
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
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...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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.