| C Tutorial | Array names are const | |||||||||
|
Advanced C Arrays Plus Syntax Pointer Style and strcpy() Pointer Type Effects Arrays and Pointers Array Names Are Const Heap Memory Memory Management Dynamic Arrays Advantages of being in the heap Disadvantages of being in the heap Dynamic Strings
|
One subtle distinction between an array and a pointer, is that the pointer which represents the base address of an array cannot be changed in the code. The array base address behaves like a const pointer. The constraint applies to the name of the array where it is declared in the code-- the variable ints in the example below.
{ int ints[100] int *p; int i;
ints = NULL; // NO, cannot change the base addr ptr
ints++; // NO
p = ints; // OK, p is a regular pointer which can be changed // here it is getting a copy of the ints pointer
p++; // OK, p can still be changed (and ints cannot) p = NULL; // OK p = &i; // OK
foo(ints); // OK (possible foo definitions are below) } Array parameters are passed as pointers. The following two definitions of foo look different, but to the compiler they mean exactly the same thing. It's preferable to use whichever syntax is more accurate for readability. If the pointer coming in really is the base address of a whole array, then use [ ].
void foo(int arrayParam[]) { arrayParam = NULL; // Silly but valid. Just changes the local pointer }
void foo(int *arrayParam) { arrayParam = NULL; // ditto }
Want To Know more with Video ??? Contact for more learning: webmaster@freehost7com |
|