diff between array name and its base adress

  • Thread starter ramasubramanian.rahul
  • Start date
R

ramasubramanian.rahul

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
 
I

Ian Collins

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.
 
S

shaanxxx

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;
}
 
L

lovecreatesbea...

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.
 
J

Jack Klein

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top