473,387 Members | 1,700 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,387 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 14989
浩澄 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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.