Why C arrays are not pointers? Well, by definition
In C, there is a strong relationship between pointers and arrays, strong enough that pointers and arrays should be discussed simultaneously. (K&R)They are easy to mix up
When an array name is passed to a function, what is passed is the location of the initial element. (K&R)An example
#include <stdio.h>
void func(char subarray[100], char* pointer) {
printf("sizeof subarray=%zd\n", sizeof(subarray));
printf("address of subarray=%p\n", (void *)subarray);
printf("pointer=%p\n", (void *)pointer);
}
int main() {
char array[100];
printf("sizeof of array=%zd\n", sizeof(array));
printf("address of array=%p\n", (void *)array);
printf("address of array[0]=%p\n", (void *)&array[0]);
func(array, array);
}
// ----------------------------------------
sizeof of array=100
address of array=0xbfbfe760
address of array[0]=0xbfbfe760
sizeof subarray=4
address of subarray=0xbfbfe760
pointer=0xbfbfe760
No comments:
Post a Comment