I've got confused when learning array and pointer in C language,why are ch, &ch,&ch[0] just equal to one another ,while sptr, &sptr,&sptr[0] are not? Here's my source code:
int main(void)
{
char ch[7] = { '1','2','3','4','5','6','0' };
char *sptr = "132456";
double db[4] = { 1.0,2.0,3.0,4.0 };
printf("\n%p %p %p\n", ch, &ch,&ch[0]);
printf("\n%p %p %p\n", sptr, &sptr,&sptr[0]);
printf("\n%p %p %p\n", db, &db,&db[0]);
return 0;
}
And the inputs on my machine are:
00FDFD68 00FDFD68 00FDFD68
00037CD0 00FDFD5C 00037CD0
00FDFD34 00FDFD34 00FDFD34
What is the difference between int[] a and int a[] in Java? There is no difference in these two types of array declaration. There is no such difference in between these two types of array declaration. It's just what you prefer to use, both are integer type arrays.
There are three different kinds of arrays: indexed arrays, multidimensional arrays, and associative arrays.
Array refers to a collection consisting of elements of homogeneous data type. Structure refers to a collection consisting of elements of heterogeneous data type. Array is pointer as it points to the first element of the collection. Instantiation of Array objects is not possible.
A stack is a type of linear data structure that is represented by a collection of pieces that are arranged in a predetermined sequence. An array is a collection of data values that are associated to one another and termed elements. Each element is recognized by an indexed array.
In most (though not all) contexts an array "decays" into a pointer to its first element. This is why ch
and &ch[0]
are the same in your example (array element access has higher precedence than the "address of" operator, so the latter could also be written as &(ch[0])
).
The remaining &ch
is one case where the array does not decay into a pointer; instead, you get the address of the array. Naturally enough, this is the same as the address of the first element of the array - however, importantly, it has a different type; it is of type char (*)[7]
, i.e. a pointer to an array of char with 7 elements. The other two pointers are of type char *
, i.e. a pointer to an individual char
.
Since sptr
is a pointer, &sptr
is the address of that pointer and naturally will be different. &sptr[0]
is equivalent to sptr + 0
, which is of course equal to sptr
.
That you do not see why sptr
and &sptr
yield different addresses indicates a misunderstanding of what a pointer is. A pointer is a fixed-size object with a value that can refer to (point at) some arbitrary object of a particular type. Because it is an object itself, a pointer can be made to point at a different object. An array variable, on the other hand, always (during its lifetime) refers to the same array object.
In your example output:
00037CD0 00FDFD5C 00037CD0
The first value, 00037CD0, is the location to which sptr
points - that is, it is the location in memory of the string constant "132456". The second value, 00FDFD5C, is the address of the sptr
variable itself. What this shows is that there is a pointer object at address 00FDFD5C which holds the value 00037CD0.
Essentially, the difference between the two cases boils down to this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With