By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

diff between array name and its base adress

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.