473,320 Members | 1,916 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.

Why the address of array equals array.

Hi All C gurus:
Below is a small program to print out the address of array and
address of array variable:

int main() {
char array[8] = "haha";
printf("array:%x\n", array);
printf("&array:%x\n", &array);
}

The result is :
array:bfffde80
&array:bfffde80

In my thoughts, the "array" and "&array" should be different and why
the
printed out values are the same.
Please help to clarify my doubts? Is there anything I missed in C
language
definition?

Mar 14 '07 #1
8 14984
浩澄 wrote:
Hi All C gurus:
Below is a small program to print out the address of array and
address of array variable:

int main() {
char array[8] = "haha";
printf("array:%x\n", array);
printf("&array:%x\n", &array);
}

The result is :
array:bfffde80
&array:bfffde80

In my thoughts, the "array" and "&array" should be different
They are different, but in your implementation, that difference
isn't visible when you print them out.
and why the printed out values are the same.
(a) you were (un)lucky: what you did has undefined behaviour, since
you're printing a pointer out using a format that expects an
int. Anything can happen (not just in the next half-hour) ...

(b) ... but, assuming that pointers-to-char and ints have the same
size, and that the representation of pointers is just some
canonical address, you printed out two equal pointers and got
to equal answers.

--
Chris "electric hedgehog" Dollin
"We live for the One, you die for the One." Unsaid /Babylon 5/.

Mar 14 '07 #2
?? said:
Hi All C gurus:
Below is a small program to print out the address of array and
address of array variable:

int main() {
char array[8] = "haha";
printf("array:%x\n", array);
printf("&array:%x\n", &array);
}

The result is :
array:bfffde80
&array:bfffde80
You forgot to #include <stdio.hso there is no prototype for the
variadic function, printf - so any output you get is untrustworthy.
Nevertheless, let's run with what you've got...
>
In my thoughts, the "array" and "&array" should be different
Good, because they /are/ different. They have different types. The first
has the type char *, and the second has the type char (*)[8].
and why the printed out values are the same.
For much the same reason that:

1 Tennis Court Road

and

Tennis Court Road

can both be found at 52/12/10N 0/7/15E.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 14 '07 #3
Array name is the representation of the address of the first element
of the array.
Mar 14 '07 #4
On Mar 14, 7:04Â*am, "浩澄" <houch...@gmail.comwrote:
Hi All C gurus:
Â* Â*Below is a small program to print out the address of array and
address of array variable:

int main() {
Â* Â* char array[8] = "haha";
Â* Â* printf("array:%x\n", array);
Â* Â* printf("&array:%x\n", &array);
Others have pointed out the problems here; make sure you #include
<stdio.hand rewrite the print statements as

printf("array: %p\n", (void*) array);
printf("&array: %p\n", (void*) array);

The %p conversion specifier is used to print out pointer values, and
it expects arguments of type void*.
}

The result is :
array:bfffde80
&array:bfffde80

In my thoughts, the "array" and "&array" should be different and why
the
printed out values are the same.
They are different types; however, they wind up being the same value
because the address of the array is the same as the address of the
first element in the array.

Apart from a couple of exceptions, when an array identifier appears in
an expression, its type is converted from "N-element array of T" to
"pointer to T", or T*, and its value is the address of the first
element of the array (&arr[0]).

The exceptions to this rule are when the array identifier is an
operand of either the sizeof or address-of (&) operators. When the
array is an operand of the & operator, the result is of type "pointer
to N-element array of T", or T (*a)[N], and its value is the address
of the base of the array, which is the same as the address of the
first element of the array.
Please help to clarify my doubts? Is there anything I missed in C
language
definition?

Mar 14 '07 #5
Matthew Zhou wrote:
Array name is the representation of the address of the first element
of the array.
No, it is not. The name of an array is /not/ a pointer to its first
element: it is /converted/ to that when it appears in a value context.

[Consider the action of the operators `sizeof` and `&`.]

--
Chris "electric hedgehog" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Mar 14 '07 #6
On Mar 14, 7:27 pm, Matthew Zhou <zhouyan1...@gmail.comwrote:
Array name is the representation of the address of the first element
of the array.
Hi,
its like array and &(array+0) or &array[0] are same.
Thanks.

Mar 14 '07 #7
Rahul wrote:
On Mar 14, 7:27 pm, Matthew Zhou <zhouyan1...@gmail.comwrote:
>Array name is the representation of the address of the first element
of the array.

Hi,
its like array and &(array+0) or &array[0] are same.
Yes, exactly like that: both statements are false.

(`&(array+0)` isn't legal; you probably mean to leave
out the `&` or put in `*`, yes?)

--
Chris "starring hedgehog" Dollin
Nit-picking is best done among friends.

Mar 14 '07 #8
Thanks all gurus a lot!
I got it, the address of "pointer of array" should be equal to address
of "the pointer to first array element".
Thought they are in different types!
1 Tennis Court Road
and
Tennis Court Road
is really an interesting way to see the C language :)


Mar 15 '07 #9

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

Similar topics

5
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with...
15
by: Carramba | last post by:
hi! I am trying to confirm if input is digit, so I thought it would by easy to do it with with isdigit() funktion, but how do I pass arrays to it if the imput is more then 1 sign? #include...
10
by: Eranga | last post by:
I have the following code; string test1 = words;//"Exclud" string test2 ="Exclud"; string test3 = String.Copy(words);//"Exclud" bool booTest1 = test1.Equals(test2);//false bool booTest2 =...
18
by: Doug Bell | last post by:
Hi, I have a function that loads Rows from a field in a DataTable into an ArrayList. See below: It also adds a Row (eg "All Areas"). I wanted this new row to be the first record so I added a...
3
by: Dan | last post by:
Hi Does anyone know how to get the Mac address of a remote PC, using C#? I believe it's possible by deconstructing the packet received from a ping but I don't know how to do it. TIA Dan
15
by: arne.muller | last post by:
Hello, I was wondering if the following statement will initialize all the 6 elements with NULL: void function() { int *list = {NULL}; . .. }
14
by: NetworkElf | last post by:
Hi all, Does anyone have some code that shows an example of how to loop through a range of IP addresses? I'm using text boxes to get a start and end value for the range. I was thinking about...
4
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates...
3
dlite922
by: dlite922 | last post by:
I have a list of objects, the object has a String and an Int in it. I need to check to see if an item is in the list, if so increment the integer and save it back to the list. I believe I've done...
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...
0
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
0
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...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.