Tuesday, May 03, 2011

C: Two dimensional arrays and pointer indexing

Pointer indexing of two dimensional array:
my2DArray[iCol][iRow] =
*(mpPointer+iRow*nCol+iCol);
"The C Programming Language", 2nd edition, Brian W. Kernighan, Dennis M. Ritchie:
[p.29] When the name of an array is used as an argument, the value passed to the function is the location or address of the beginning of the array - there is no copying of array elements. By subscripting this value, the function can access and alter any argument of the array.

[p.78] The unary operator & gives the address of an object, so the statement "p = &c;" assigns the address of c to the variable p, and p is said to "point to" c... The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to.

[p.83] By definition, the value of a variable or expression of type array is the address of element zero of the array... a reference to a[i] can also be written as *(a+i)... In short, an array-and-index expression is equivalent to one written as a pointer and offset... There is one difference between an array name and a pointer that must be kept in mind. A pointer is a variable, so pa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal.

[p.83] As formal parameters in a function definition, "char s[];" and "char *s;" are equivalent...

No comments: