#include<stdio.h>
int main()
{
int a[5]={5,10,15,20,25};
int *p;
int i;
p=a;
for (i=0;i<=5;i++)
{
printf("the address of the %d is = %dn",*p,p);
p++;
}
return 0;
}
In my program i do not even know that what was happening there because if put the address of operator in seventh line of the code which is =(p=&a)
else I do not put = (p=a)
the address of operator the answer of the running time of the both the case are same.
I was expecting that may be the answer is different in both cases but on both time the answer is same.
khushal dak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
For starters this for loop
for (i=0;i<=5;i++)
{
printf("the address of the %d is = %dn",*p,p);
p++;
}
invokes undefined behavior because the valid range of elements of the array a
is [0, 5 )
instead of [0, 5]
. That is when i
is equal to 5
the expression *p
tries to access memory outside the array a
that results in undefined behavior. And to output a pointer you should use conversion specifier p
instead of d
. Otherwise your code will have again undefined behavior.
At least you should write the for loop like
for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
{
//...
}
As for your question then the address of an array and the address of its first element are the same. Though relative to your code expressions a
and &a
used as initializers in this statement like
p=a;
or
p=&a;
have differernt types. The expression a
has the type int *
while the expression &a
has the type int ( * )[5]
. So for this statement
p=&a;
the compiler should issue an error or warning message that there are used incompatible pointer types.
Pay attention to that arrays used in expressions with rare exceptions (as for example used with the sizeof
operator) are implicitly converted to pointers to their first elements.
i do not even know that what was happening there because if put the
address of operator in seventh line of the code which is =(p=&a) else
I do not put = (p=a) the address of operator the answer of the running
time of the both the case are same.
It is because arrays decay to the reference to its first element. When you use &
you get the same reference but it has a different type.
int main(void)
{
int array[5];
int *pointer_to_int = array;
int *pointer_to_int1 = &array[0];
int (*pointer_to_array)[5] = &array;
printf("%p %p %p %p %zun", (void *)pointer_to_int, (void *)pointer_to_int1, (void *)(pointer_to_int + 1), (void *)(pointer_to_int1 + 1), sizeof(*pointer_to_int));
printf("%p %p %zun", (void *)pointer_to_array, (void *)(pointer_to_array + 1), sizeof(*pointer_to_array));
}
pointer_to_int
&pointer_to_int1
have type of pointer toint
and reference first element of thearray
pointer_to_array
has type of pointer to array of 5int
elements.
When you run the posted program you will get the result (the actual addresses can be different):
0x7ffef9ea1320 0x7ffef9ea1320 0x7ffef9ea1324 0x7ffef9ea1324 4
0x7ffef9ea1320 0x7ffef9ea1334 20
as you can see the pointer arithmetics behaves differently.