fact is any array of anything is just a pointer. The array syntax just allows you the functionality to associate memory with that pointer. Since they're both pointers the syntax for addressing them is interchangable.Code:char *ptr = "zipy zippy zoo"; char ptr2[] = "blah blah blah"; char ptr3[15] = "zip zip bip"; foo ( char *myptr) { printf( "%s\n", myptr ); } int main() { char *ptr4 = new[15]; strcpy( ptr4, "shazam"); foo(ptr); foo(ptr2); foo(ptr3) foo(ptr4); delete []ptr4; }
base[] == *base derefferenceCode:typedef struct MYSTRUCT { int i; char c; char z[20]; } MYSTRUCT, *PMYSTRUCT; int main() { MYSTRUCT myArray[10]; PMYSTRUCT pMyArray = new MYSTRUCT[10]; myArray->i = 1; // set's the first value of the array pMyArray->i = 1; // set's the first value of the pointer memory (myArray+2)->i = 1; // set's the third item of the array (pMyArray+2)->i = 1; // set's the third item of the pointed to memory myArray[2].i = 3; // set's the third item of the array pMyArray[2].i = 3; // set's the third item of the pointed to memory delete []pMyArray; }
base[#] == *(base+offset) add an offset to the base and dereffrence
arrays are just pointers with memory already associated with them. Pointers are just arrays which may or may not have the memory associated with them.
The only difference I know of is one which SuperKoko touched upon..
You can't change the base address of an array. Pointers you can..
char szbuffer[10] = "";
char *pszBuffer = new char[10];
szBuffer += 1; // ilegal
pszBuffer += 1; // legal
An array contains an address, and a pointer contains an address.
The rest is just syntax.




Reply With Quote