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

int a[2][3]; What does "a" actually contain?

consider the program,
/************************************************** *******/
int
main()
{
int a[2][3];
printf("a = %u\n", a);
printf("*a = %u\n", *a);
return 0;
}
/************************************************** *********/

When I compiled and executed the above program, I got the following
output:
a = 3213021068
*a = 3213021068

As you can see both "a" and "*a" give the same value; Why is this
happening? What value does "a" acutally contain? (I mean to whom does
it point to?)

Aug 5 '06 #1
5 2165
I have encounted the same situation.

int main()
{
int a[2][3];
printf("&a=%u\n",&a);
printf("a=%u\n",a);
printf("*a=%u\n",*a);
printf("a[0]=%u\n",a[0]);
printf("&a[0]=%u\n",&a[0]);
printf("a[1]=%u\n",a[1]);
printf("&a[1]=%u\n",&(a[1]));
printf("a[0][0]=%u\n",(a[0][0]));
printf("&a[0][0]=%u\n",&(a[0][0]));
getch();
return 0;
}

The result:

&a=2293584
a=2293584
*a=2293584
a[0]=2293584
&a[0]=2293584
a[1]=2293596
&a[1]=2293596
a[0][0]=2009118740
&a[0][0]=2293584

Aug 5 '06 #2
chandanlinster wrote:
consider the program,
/************************************************** *******/
#include <stdio.h>
int
main()
{
int a[2][3];
printf("a = %u\n", a);
printf("*a = %u\n", *a);
%u is for unsigned ints, not pointers. So you can't use this
code as the basis for _any_ decent analysis of output.

Use %p but make sure the pointer supplied to printf is a void *.

printf("a = %p\n", (void *) a);
printf("*a = %p\n", (void *) *a);
return 0;
}
/************************************************** *********/

When I compiled and executed the above program, I got the following
output:
a = 3213021068
*a = 3213021068

As you can see both "a" and "*a" give the same value; Why is this
happening? What value does "a" acutally contain? (I mean to whom
does it point to?)
The object a is an array, it is not a pointer. But when used in most
expressions, except as an operator to & or sizeof, an array will
'decay'
to a pointer to it's first element.

Thus the above code is equivalent to...

printf("a = %p\n", (void *) &a[0]);
printf("*a = %p\n", (void *) &(*a)[0]);

Since *a is just a[0], it's also equivalent to...

printf("a = %p\n", (void *) &a[0]);
printf("*a = %p\n", (void *) &a[0][0]);

Why do these two print the same thing? Answer: Why wouldn't they?
The first one prints the address of the first element of a, the second
prints the address of the first sub-element of the first element of a.

Since arrays are not 'padded', these address must be the same.

Note that there is a subtle difference in types, even if the addresses
are the same.

--
Peter

Aug 5 '06 #3
chandanlinster wrote:
int
main()
{
int a[2][3];
printf("a = %u\n", a);
printf("*a = %u\n", *a);
return 0;
}

When I compiled and executed the above program, I got the following
output:
a = 3213021068
*a = 3213021068

As you can see both "a" and "*a" give the same value;
"a" means &a[0] and "*a" means &a[0][0]. (If you aren't sure
about this, then go to http://c-faq.com/ and read chapter 6).

The first one is the address of an array, the second one is
the address of the first int in that same array -- clearly these
two are at the same location in memory.

NB. %u is not suitable for printing out memory locations,
you should use %p, and cast the argument to void* :

printf("a = %p\n", (void *)a);

Aug 5 '06 #4
In article <11*********************@i3g2000cwc.googlegroups.c om>
chandanlinster <ch************@gmail.comwrote:
int a[2][3];
>As you can see both "a" and "*a" give the same value; Why is this
happening? What value does "a" acutally contain?
The actual values contained within the object "a" are the six "int"
values you have assigned to the six elements a[0][0], a[0][1],
a[0][2], a[1][0], a[1][1], and a[1][2]. (Since you never assign
any, and "a" is an automatic-duration object, these six values
are all garbage.)

The "value of a" and "value of *a" are pointers, however, due to
The Rule about arrays and pointers in C. See
<http://web.torek.net/torek/c/expr.html#theruleand
<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.
Aug 5 '06 #5

Old Wolf wrote:
chandanlinster wrote:
int
main()
{
int a[2][3];
printf("a = %u\n", a);
printf("*a = %u\n", *a);
return 0;
}

When I compiled and executed the above program, I got the following
output:
a = 3213021068
*a = 3213021068

As you can see both "a" and "*a" give the same value;

"a" means &a[0] and "*a" means &a[0][0]. (If you aren't sure
about this, then go to http://c-faq.com/ and read chapter 6).

The first one is the address of an array, the second one is
the address of the first int in that same array -- clearly these
two are at the same location in memory.

NB. %u is not suitable for printing out memory locations,
you should use %p, and cast the argument to void* :

printf("a = %p\n", (void *)a);

may be the following exercise might be helpful

which of the following expressions are equivalent to a[ j ][ k ]

a) *( a[ j ] + k )
b) **( a[ j + k ] )
c) ( *( a + j ) )[ k ]
d) ( *( a + k ) )[ j ]
e) *( ( * ( a + j ) ) + k )
f) **( a + j ) + k
g) *( &a[0][0] + j + k)

Aug 7 '06 #6

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

Similar topics

15
by: lkrubner | last post by:
I want to give users the power to edit files from an easy interface, so I create a form and a PHP script called "fileUpdate". It does a reasonable about of error checking and prints out some...
2
by: barb28 | last post by:
Hello, sometimes I have noticed that in web addresses, the number: 20% will show up in the address. I think this is somehow related to a 'blank space', but am unsure, does anyone know about...
9
by: Xiangliang Meng | last post by:
Hi, all. I see a very strange fragment code today. Uint32 enum { a = 100; b = 200; }; NOTE: Uint32 is defined to be 'unsigned' in other source files.
11
by: L. Chen | last post by:
The standard says that a char* or void* pointer has the least strict alignment. But I do not know what is a strict alignment. What does that mean?
3
by: nsh | last post by:
mailing.database.mysql, comp.lang.php subject: does "LOAD DATA" EVER work?!? I've tried EVERYTHING! version info: my isp is running my web page on a linux box with php ver. 4.4.1 according to...
39
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the...
51
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and...
5
by: planetbrett | last post by:
I have read through php.net manuals and have not see any mention about what these operands actually do. I have seen them used in a bunch of different code lately and don't really understand. ...
3
by: Cirene | last post by:
When you right click on a page in VS and set it as the "default page" what actually takes place? How does the site know to go here first?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.