Help understanding arrays and pointers
I am learning C from a book, and I don't seem to understand fully the following paragraphs about arrays and pointers:
Although many similarities exist between pointers and arrays, they have one key difference: An array isn't a variable.
Pointers are variables. You can change their contents and change what lives at the memory addresses they point to. Arrays, on the other hand, are pretty much stuck in memory. You can't move them. You can't reassign them. You can change their variables, but that's about it.
I don't seem to understand what is in BOLD (the things about arrays). Can somebody please explain and give easy examples to the text in BOLD. Thanks.
Re: Help understanding arrays and pointers
Quote:
Originally posted by James00
Arrays, on the other hand, are pretty much stuck in memory. You can't move them. You can't reassign them. You can change their variables, but that's about it.
Code:
int a[10];
int b[10];
a = b; //ERROR
Arrays are no first class objects,you cannot assign them the simple way.
But with pointers it works
Code:
int *a = malloc(sizeof(int) * 10);
int *b = a;//OK;
Now a and b point to the same address.