473,397 Members | 2,116 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,397 software developers and data experts.

2D arrays and addresses

Hi All,

In the weekends I read a sample program from a book and think of tying
some experiments.
The code was somethng like:

#include <stdio.h>

int main()

{

char multi[3][4] =

{

{ 'A', 'B' , 'C', 'd' },
{ '1', '2' , '3', '5' },
{ 'a', 'z' , 'e' , '1' }

};

printf( " &multi[1][0] %p \n", &multi[1][0]);

printf( " multi + 1 %p \n", (multi + 1));

printf(" *(multi + 1 ) %p \n", *(multi +1));

return 0;

}

The Address for &multi[1][0] was ffbeeb2c.
In fact output of the program was same for all the printf's i.e
ffbeeb2c.
I am not able to get why

(multi + 1) and *(multi + 1 ) are showing the same output.

According to what I understood was ( multi + 1) should contain the
address and
*(multi + 1) should show the contents for that memory location.
Did I assume that ( according to output) the contents of ( multi + 1 )
is it's own address.
What is the meaning of it.

Dec 26 '06 #1
2 1138
On 25 Dec 2006 23:31:04 -0800, Co********@gmail.com wrote:
>Hi All,

In the weekends I read a sample program from a book and think of tying
some experiments.
The code was somethng like:

#include <stdio.h>

int main()

{

char multi[3][4] =

{

{ 'A', 'B' , 'C', 'd' },
{ '1', '2' , '3', '5' },
{ 'a', 'z' , 'e' , '1' }

};

printf( " &multi[1][0] %p \n", &multi[1][0]);

printf( " multi + 1 %p \n", (multi + 1));

printf(" *(multi + 1 ) %p \n", *(multi +1));

return 0;

}

The Address for &multi[1][0] was ffbeeb2c.
In fact output of the program was same for all the printf's i.e
ffbeeb2c.
I am not able to get why

(multi + 1) and *(multi + 1 ) are showing the same output.

According to what I understood was ( multi + 1) should contain the
address and
*(multi + 1) should show the contents for that memory location.
Did I assume that ( according to output) the contents of ( multi + 1 )
is it's own address.
What is the meaning of it.
1 - In most contexts (except when the operand of either the & or
sizeof operators), an expression with array type evaluates to the
address of the first element of the array with type pointer to element
type.

2 - While multi looks like a 2D array, it is actually an array of 3
arrays of 4 char. multi[0] is an array of 4 char containing 'A', 'B',
'C', and 'd'. multi[1] is an array containing '1', '2', '3', and '5'.

3 - Pointer arithmetic in c always includes an implied scaling by the
sizeof the type pointed to. If an int i occupies four bytes, then the
expression &i+1 points four bytes beyond the start of i. If a double
d occupies eight bytes, the expression &d+1 points eight bytes beyond
the start of d.

Now we apply these three "rules", recursively if necessary:

A - multi[1][0] is the element containing the '1' . It is a char. The
& operator produces the address of this element which has the type
char*. The standard requires char* and void* to have the same size
and representation. The %p expects a void* so the char* is acceptable
and you print the address of the '1'. While the char* is acceptable,
most here recommend casting the expression to void* (even if only for
consistency).

B - (multi+1) evaluates as the address of multi[0] (rule 1) plus 1.
multi[0] is an array of four char and its address is the byte that
holds the 'A' (rule 2). This address has the type pointer to array of
four char, in c notation char(*)[4]. The type pointed to is array of
four char and the sizeof this type is 4. The expression multi+1
evaluates to the address four bytes beyond the 'A' (rule 3), which is
the address of the '1'. However, adding the 1 did not change the type
which remains char(*)[4]. This is not the same type as char* and
therefore need not have the same size or representation as void*. By
applying the %p to this type, you invoke undefined behavior. You need
to cast the type to void*. Once you do this, the printf will print
the address of the '1' which is the same as A above.

C - *(multi+1) start out the same as the preceding. multi+1 is the
address of the array of four char starting with the '1'. The *
operator dereferences this address, yielding the array itself. We now
have an expression of array type. Applying rule 1 again, this
evaluates to the address of the first element ('1') with type pointer
to element type (char*). This is the same situation described in A
above.
Remove del for email
Dec 26 '06 #2
Co********@gmail.com writes:
In the weekends I read a sample program from a book and think of tying
some experiments.
The code was somethng like:

#include <stdio.h>
int main()
{
char multi[3][4] =
{
{ 'A', 'B' , 'C', 'd' },
{ '1', '2' , '3', '5' },
{ 'a', 'z' , 'e' , '1' }
};

printf( " &multi[1][0] %p \n", &multi[1][0]);
printf( " multi + 1 %p \n", (multi + 1));
printf(" *(multi + 1 ) %p \n", *(multi +1));
return 0;
}
[extraneous blank lines deleted; why do people feel the need to
double-space code?]

Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 26 '06 #3

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

Similar topics

35
by: Troll | last post by:
Hi, I need to write a script which reads some data and reports the findings. Just to give you an idea the structure is similar to the following. Data input example: HEADING 1 **********...
2
by: Adam Balgach | last post by:
Greetings everyone, ive got a problem ive been working with for quite a while and need some help. ive got a structure: struct Data { char *data1; char *data2; int val1; int val2;
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
8
by: Greg | last post by:
In VB6 I made heavy use of control arrays I see they have been 'deprecated' in vb.Net, with a questionable explanation that they are no longer necessary which just addresses the event issue!...
6
by: Michael Gray | last post by:
VS 2003 VB.net Win2000 SP4 The System.Array class seems to be limited to 32 bit addresses, meaning that one can only assign 2^32 elements. Is there any way that I can have an array that...
38
by: Peteroid | last post by:
I looked at the addresses in an 'array<>' during debug and noticed that the addresses were contiguous. Is this guaranteed, or just something it does if it can? PS = VS C++.NET 2005 Express...
2
by: MarkD | last post by:
Hi All, I have a system that stores information about a person, name addresses and so on and I would like to return a single row for each person in an SQL statement. This is so it can be placed in...
4
by: selam | last post by:
I have this assignment using c++ please help me in writing a code. Suppose you have a main ( ) with three local arrays, all the same size and type (say float). The first two are already initialized...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
11
by: Sunny | last post by:
#include <iostream> int main() { int len; std::cin >len; int Arr; int *p = new int; Arr=5; std::cout << &len << " " << &Arr << " " << p << std::endl;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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
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...

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.