Below are 2 programs
First
#include<stdio.h>
void main()
{
int a[5]={1,2,3,4,5};
int *p;
p=&a;
printf("%u %u",p,p+1);
}
Second
#include<stdio.h>
void main()
{
int a[5]={1,2,3,4,5};
printf("%u %u",&a,&a+1);
}
Now, in the two programs..I have printed the values of &a using p in first code and directly in the 开发者_开发百科second..
Why are the results different?
the answer i m getting are.
for first 3219048884 3219048888
for second 3219048884 3219048904
The type of &a
is int (*) [5]
. Therefore &a+1
is a pointer that's 5 int
s further on than a
. However the type of p
is int *
, therefore p+1
is a pointer that's 1 int
further on than p
.
When I run that, I get this:
1245036 1245040 1245036 1245040
1245036 1245040 1245036 1245056
with the only difference being in the last position, p+1
vs &a+1
p
is a pointer to an integer, so p+1
is the address of the next integer. (i.e 4 byte further in memeory)
a
is an array of 5 integers, so &a+1 is the address of the next array of 5 integers. (i.e., 20 bytes further in memeory)
In both programs, you are printing the memory addresses of the array.
This can vary each time you run the program. The memory that the OS chooses to give you can be different.
When your program declares an array of 5 integers, The OS promises to give you 5 consecutive integers, it does NOT promise what memory you will get, or that you will get the same memory every time.
You have two programs with different stack frames, no surprise the addresses of local variables are different. Il may change each time you run the program (that's what it does when I try it, code compiled with gcc on Linux).
But you'll get the same values with the program below one except for the last value of the serie (except for the last one, because of the way pointer arithmetic works).
#include<stdio.h>
void main()
{
int a[5]={1,2,3,4,5};
int *p;
p=&a;
printf("%u %u %u %u ",a,a+1,p,p+1);
printf("%u %u %u %u",a,a+1,&a,&a+1);
}
For a (quite) complete explanation of difference between pointers and arrays you can look at my answer here
Your first program is invalid. It is illegal to assign p = &a
, since p
has type int *
and &a
has type int (*)[5]
. These types are not compatible. If your compiler is loose enough to allow this kind of assignment (did it at least warn you?), it probably interpreted it as p = (int *) &a
, i.e. it forcefully reinterprets the value of &a
as an int *
pointer. Thus, all pointer arithmetic in your first program is int *
arithmetic. This is why the first program produces the same output for a+1
and p+1
values.
In the second program the value of &a
is not reinterpreted. It keeps its original type ``int ()[5]and it follows the rules of normal pointer arithmetic for type
int ()[5], meaning that when you do
&a + 1, the pointer is moved
sizeof(int[5])` bytes, as it should. This is why the result is different from the first program.
You should use p = a
and not p = &a
. An array like a
is already assumed a constant pointer. You do not need to dereference it with the &
operator.
精彩评论