473,503 Members | 756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

diff between array name and its base adress

if we have a declaration
int arr[] = { 1,2,3,4,5};
int *ptr;
what is the diff betn ptr = &a and ptr = a ;

when i do
ptr = &a ;
or ptr = a ;
and then ptr += 2 ;
then *ptr points to 3 in both cases
Basically , my doubt is whether the statement " array name is same as
the address of array " true in all cases without exception and can both
be used interchangebly ..??
kind regards
rahul

Sep 26 '06 #1
4 2661
ra*******************@gmail.com wrote:
if we have a declaration
int arr[] = { 1,2,3,4,5};
int *ptr;
what is the diff betn ptr = &a and ptr = a ;
See the thread 'C Strings not returning from a function' from earlier today.

--
Ian Collins.
Sep 26 '06 #2

ra*******************@gmail.com wrote:
if we have a declaration
int arr[] = { 1,2,3,4,5};
int *ptr;
what is the diff betn ptr = &a and ptr = a ;
You will get warning in 'ptr = &a'
"warning: initialization from incompatible pointer type"

and you wont get warning in 'ptr = a'

Basically, &a return pointer to array of integer of size 5. And , a
return pointer to integer.

We can experiment this by following statements;

( (unsigned long )(&a + 1) - (unsigned long )(&a) ) , this will
return 5 * 4 = 20 (Assume : sizeof(int )= 4 )

and

( (unsigned long )(a + 1) - (unsigned long )(a) ) will return 4;

#include<stdio.h>
int main()
{
int a[5];
int *ptr = a;
int *ptr1 = &a ;/*warning: initialization from incompatible
pointer type*/
int (*ptr2)[5] = &a;
printf("%d\n",(int) ( (unsigned long )(&a + 1) - (unsigned
long )(&a) ) );
printf("%d\n",(int) ( (unsigned long )(a + 1 ) - (unsigned
long )(a ) ) );
return 0;
}

Sep 26 '06 #3

ra*******************@gmail.com wrote:
if we have a declaration
int arr[] = { 1,2,3,4,5};
int *ptr;
what is the diff betn ptr = &a and ptr = a ;
Their types. See c-faq.com, sec 6.12.
when i do
ptr = &a ;
or ptr = a ;
and then ptr += 2 ;
then *ptr points to 3 in both cases
Basically , my doubt is whether the statement " array name is same as
the address of array " true in all cases without exception and can both
be used interchangebly ..??
Array name in expression is the address its initial element in some
context.

Sep 26 '06 #4
On 25 Sep 2006 22:23:27 -0700, "ra*******************@gmail.com"
<ra*******************@gmail.comwrote in comp.lang.c:
if we have a declaration
int arr[] = { 1,2,3,4,5};
int *ptr;
what is the diff betn ptr = &a and ptr = a ;

when i do
ptr = &a ;
If you code this, your compiler is required to issue a diagnostic. If
it does not, your compiler is not (or is not being invoked in) a
standard conforming mode.
or ptr = a ;
and then ptr += 2 ;
then *ptr points to 3 in both cases
Basically , my doubt is whether the statement " array name is same as
the address of array " true in all cases without exception and can both
Who made the (incorrect) statement that " array name is the same as
address of the array "? What are their qualifications to make such
statements? Why should we believe him/her/them? He/she/they are
quite wrong in this case.
be used interchangebly ..??
kind regards
rahul
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 26 '06 #5

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

Similar topics

10
2292
by: BadOmen | last post by:
I have made an array in a function in a Class now I need to get to that Array from an other function in an other Class, How do I do that? The Array has no fixed size as it is dependent on how...
9
1892
by: jlopes | last post by:
There seems to bet no diff between a vitual method and an inheirited method. class A_Base { public: virtual void filter(){ /* some code */ } }; class D_of_A_Base : public A_Base {
11
8886
by: iceColdFire | last post by:
Hi, What is the Diff btwn Function overloading and overriding thanks, a.a.cpp
13
1573
by: Martin Altschwager | last post by:
Hi, I desperately need some help with what I assume to be a minor problem that I just can't solve. I'm writing a page where on mouseover a link, an image needs to be displayed and some text,...
8
2356
by: ehames | last post by:
Hi guys, As far as I know, for any type T, and positive integer N, if I have an array declared as: T array; then, &array and array are the same element. Is there any reason why a
4
4933
by: Andreas Kasparek | last post by:
Hola! I'm preparing my master thesis about a XML Merge Tool implementation and was wondering if there is any open standard for XML diff regarding topics like: - is a diff result computed on...
3
2945
by: Csaba Gabor | last post by:
I'm comparing the text of (snippets of) web pages which I expect to be quite different or quite similar. In the case where they are similar, I would like to display the more recent one and say...
1
1497
by: dadevil | last post by:
Hi guys, this is my first post around here, and here it goes I need to create an array to store info about a number "n" of persons, so i create this structure ( each person have one name and one...
4
6484
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be...
0
7282
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
7339
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
7463
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
4678
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...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
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 ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.