473,608 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

int a[5] .. difference between a and &a

#include<stdio. h>
int main(int argc,char **argv){
int a[5]={1,2,3,4,5};
printf("%p\n%p\ n",a,&a);
return 0;
}
gives me :
0xbfe837a0
0xbfe837a0
this means that a and its address is same ?? Please explain ..

Dec 9 '06 #1
33 2254
onkar wrote:
#include<stdio. h>
int main(int argc,char **argv){
int a[5]={1,2,3,4,5};
printf("%p\n%p\ n",a,&a);
return 0;
}
gives me :
0xbfe837a0
0xbfe837a0
this means that a and its address is same ?? Please explain ..
See comp.lang.c FAQ, 6.12: http://c-faq.com/aryptr/aryvsadr.html
(You can also write &a[0])
--
WYCIWYG - what you C is what you get

Dec 9 '06 #2
onkar said:
#include<stdio. h>
int main(int argc,char **argv){
int a[5]={1,2,3,4,5};
printf("%p\n%p\ n",a,&a);
return 0;
}
gives me :
0xbfe837a0
0xbfe837a0
Invoking undefined behaviour can give any result whatsoever.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 9 '06 #3
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:7r******** *************** *******@bt.com. ..
onkar said:
>#include<stdio .h>
int main(int argc,char **argv){
int a[5]={1,2,3,4,5};
printf("%p\n%p\ n",a,&a);
return 0;
}
gives me :
0xbfe837a0
0xbfe837a0

Invoking undefined behaviour can give any result whatsoever.
Seems to me that a decays to an int * while &a is int[5] *.
Both are well formed and happen to have the same address value,
just different types. The only thing undefined about this
program is that %p strictly speaking accepts just a void *,
which *might* have a different representation than int * or
int[5] *. Add the casts and I'll bet you get the same printout,
in a well formed program.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Dec 9 '06 #4
P.J. Plauger said:
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:7r******** *************** *******@bt.com. ..
>onkar said:
>>#include<stdi o.h>
int main(int argc,char **argv){
int a[5]={1,2,3,4,5};
printf("%p\n%p\ n",a,&a);
return 0;
}
gives me :
0xbfe837a0
0xbfe837a0

Invoking undefined behaviour can give any result whatsoever.

Seems to me that a decays to an int * while &a is int[5] *.
Well, int (*)[5], but yes. Neither is of type void *, however.
Both are well formed and happen to have the same address value,
just different types. The only thing undefined about this
program is that %p strictly speaking accepts just a void *,
which *might* have a different representation than int * or
int[5] *. Add the casts and I'll bet you get the same printout,
in a well formed program.
And what lesson will the OP draw from your apparent condoning of the cast
omission, I wonder?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 9 '06 #5
onkar wrote:
#include<stdio. h>
int main(int argc,char **argv){
int a[5]={1,2,3,4,5};
printf("%p\n%p\ n",a,&a);
return 0;
}
gives me :
0xbfe837a0
0xbfe837a0
this means that a and its address is same ?? Please explain ..
See comp.lang.c FAQ, 6.12: http://c-faq.com/aryptr/aryvsadr.html
(You can also write &a[0])
--
WYCIWYG - what you C is what you get

Dec 9 '06 #6
"onkar" <on*******@gmai l.comwrote in message
news:11******** **************@ 80g2000cwy.goog legroups.com...
#include<stdio. h>
int main(int argc,char **argv){
int a[5]={1,2,3,4,5};
printf("%p\n%p\ n",a,&a);
return 0;
}
gives me :
0xbfe837a0
0xbfe837a0
this means that a and its address is same ?? Please explain ..

No.
a is a pointer to an address where the array a[] begins. a[x] is thus an element
of that field, and the address of that element is calculated like this:

address = a + x * sizeof(int); //int is interchangeable here, depends on what
type of array you are using

&a doesn't mean anything. Since a is a pointer itself, there is no pointer to
that pointer (unless you explicitly declared there to be one). Pointer addresses
are remembered internally by the compiler and when the compiled program loads
into memory, they can be located on predefined places, depending on which part
of memory was the program loaded to. You have no access to these, and you don't
even need them. Most often, however, their relative position in the memory is
encoded into the very (binary) code of the program, so it's near to impossible
to extract them.

&a is therefore, not defined and it can be anything.

--
"It is easy in the world to live after the world's oppinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/

Dec 9 '06 #7
"Sourcerer" <en****@MAKNIgm ail.comwrote in message
news:el******** *@ss408.t-com.hr...
>
address = a + x * sizeof(int); //int is interchangeable here, depends on what
type of array you are using
Just remembered, C language "knows" the size of an array element, so the address
is calculated thus:

address = a + x;

This will move the pointer 'address' to the x-th element in any type of array
'a', thanks to the programmers of the compiler who have made our lives easier in
this aspect of C.

--
"It is easy in the world to live after the world's oppinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/

Dec 9 '06 #8
Sourcerer said:
"onkar" <on*******@gmai l.comwrote in message
news:11******** **************@ 80g2000cwy.goog legroups.com...
>#include<stdio .h>
int main(int argc,char **argv){
int a[5]={1,2,3,4,5};
printf("%p\n%p\ n",a,&a);
return 0;
}
gives me :
0xbfe837a0
0xbfe837a0
this means that a and its address is same ?? Please explain ..


No.
a is a pointer to an address where the array a[] begins.
Strictly, it is an array that, when evaluated, yields as its value the
address of its first element.

<snip>
&a doesn't mean anything. Since a is a pointer itself,
But it isn't a pointer - it's an array name. When used as an operand to &
(or to sizeof) it is not evaluated, and thus does not decay into a pointer
value.

&a means "the address of the array of 5 ints named a".

<incorrect deductions snipped>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 9 '06 #9
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:Xa******** *************** *******@bt.com. ..
Sourcerer said:
>>

No.
a is a pointer to an address where the array a[] begins.

Strictly, it is an array that, when evaluated, yields as its value the
address of its first element.
OK, that means that evaluated a = &a. &a is then the address of the array which
I earlier considered 'a' to be.
Only I have not been consistent in another matter earlier. &a isn't a pointer
(see example below). It is the value directly encoded into the binary processor
commands.
>&a doesn't mean anything. Since a is a pointer itself,

But it isn't a pointer - it's an array name. When used as an operand to &
(or to sizeof) it is not evaluated, and thus does not decay into a pointer
value.

<snip>

&a means "the address of the array of 5 ints named a".
Hmm... interesting how this subtlety affects program execution. Consider the
following program:
#include <stdio.h>
int main(void){
int a[5];
printf("%d, %d\n", sizeof(a), sizeof(&a));
return 0;
}

I first assumed this should give the output (if sizeof(int) = 4):
20, 4

This would be true if &a was a pointer, but it isn't. Execution of this program
gives the output:
20, 20

Why is the second value 20 as well? What exactly is
sizeof(&anythin g_that_is_not_a _pointer)?

--
"It is easy in the world to live after the world's oppinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/

Dec 9 '06 #10

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

Similar topics

13
3027
by: Mattias Campe | last post by:
Hi, Depending on if I get an image or a text of a certain URL, I want to do something different. I don't know in advance whether I'll get an image or a text. This is a URL that returns an image: http://indicator.amessage.info/indicator.php?param1=cobnet%40jabber.org&amp;param2=bounce&amp;param3=http%3A%2F%2Fstudent.ugent.be%2Fastrid%2Fpics%2Fjabber%2F&amp;param4=.png
1
1889
by: learning_C++ | last post by:
Hi, I compiled some code. In the function friend ostream& operator<<(ostream& os, const complex c); I use the later argument complex c and complex& c. I can get the same values and there is no error when I compile this code. Please tell me the difference. #include <iostream>
4
1892
by: Joel | last post by:
Run this method: public void test() { bool b; int i=0; b=false; i=0; b=(b && i++==1);
12
2436
by: Tee | last post by:
String Builder & String, what's the difference. and when to use which ? Thanks.
12
17375
by: Nathan Sokalski | last post by:
What is the difference between the Page_Init and Page_Load events? When I was debugging my code, they both seemed to get triggered on every postback. I am assuming that there is some difference, and I would like to know what it is so that I can take advantage of it in my code. Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
9
1965
by: Anoj | last post by:
Hi All, is there any performance difference between + and & operator while concating string litrels. which one is better and why?? Thanx
4
13792
by: VIKAS17786 | last post by:
COULD YOU EXPLAIN ME WHAT IS DIFFERENCE BETWEEN ORACLE 8i AND ORACLE 9i ? ........................... ALSO WHAT IS DIFFERENCE BETWEEN DBMS AND RDBMS ? ..... I WOULD BE WAITING FOR THE ANSWERS. THANKYOU.............
3
7406
by: PicO | last post by:
i need some explanation about the difference between priority queue & set & heap ... as they all sort the data in ( n log n ) ... but the only i see that priority queue only can pop the top ( maximum element ) while set and heap can erase any element ...
1
10600
by: sandeep kumar shah | last post by:
Hi friends, I have a problem while parsing an xml file. When the value of an attribute contains �D; it gives error but when we use &#xD; it's ok.. Could anyone plz tell me what is the difference between these two. <person name="sandeep �D; kumar"/> //cause error <person name="sandeep &#xD; kumar"/> //ok
2
4103
by: qwedster | last post by:
Folks! What is the difference between PostBack and Callback ( !IsPostBack and if(!IsCallback)) Like in the following Code Snippet: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) {
0
8063
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8496
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8475
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8148
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8338
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6816
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6013
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.