Logged in as: guest Log in | |||||||
Home | Research | Courses | Publications | ||||
Arrays, Pointers, and StructuresSeptember 25, 2015Prelab (complete before lab)Arrays are multielement variables with a uniform type, which are stored in sequential memory locations. An array variable refers to the address of the first array element called its 'base address'. Other elements are accessed by indexing from from the first element using the bracket operators '[expression]'. Since all array elements are of the same type, and are therefore the same size, array offsets can be calculated as follows: element_address = base_address + sizeof(array_type) * index There is no range checking of an array index in C. Thus, there is no need to know the size of an array when computing offsets. Multidimensional arrays are specified by fixing the outer-most 'stride' sizes. For example one can specify a 4 by 3 array with 12 elements as follows:
In terms of style, it might be preferable to declare The elements of multidimensional arrays are referenced using multiple indices. Just like a one-dimensional array, the array variable refers to the base address of the first array element. Array offsets into a two-dimensional array of the form a[j][i] are calculated as follows: element_address = base_address + sizeof(array_type) * (j * i_stride + i) While array offsets into a three-dimensional array of the form a[k][j][i] are calculated as follows: element_address = base_address + sizeof(array_type) * ((k * j_stride + j) * i_stride + i) Notice that in multidimensional arrays all 'outer strides' must be known in order to compute the array element's address, but the inner-most stride is never used to compute an element's address. Pointer variables are a powerful and much maligned feature of the C programming language. The use of pointers is often criticized since it is impossible to identify to what a pointer references without tracing code back to where it was last assigned. Moreover, pointers can reference invalid or deallocated variables. Rather than restricting or obfuscating pointer variables as other languages do, C often makes them the method of choice. In particular, pointers are commonly used to process multielement data types; in particular, arrays and structures. Pointer variables are declared with a "*" (star) prefix, and they contain addresses. As previously mentioned, array variables are also addresses. Thus, it is possible to assign the base address of an array to a pointer variable. For example: int *p = a;
References to memory using p can be made either with or without offset. Note that the variable p does not inherit with any strides associated with a's declaration. C also provides the "&" operator that gives the address of an arbitrary variable.
When the star operator is used as a prefix in an expression, as in the second statement above, it refers to the contents of the memory address of the pointer variable to its right.This can be combined with offsets as shown in the third statement. Structs are another multielement variable type provided in C. The elements of a C struct are named and need not all be of the same type. Stucts are a convenient way of aggregating groups of variables. Structs are defined as follows:
Similar to arrays the variable "thing" is the address of the structure, and elements of the structure are referenced using the "." operator as follows:
One can also define struct pointers and arrays of structs. For example
Whose elements can be set as follows:
As you can see, accessing the elements of a structure using a struct pointer is syntactically cumbersome. To deal with this C provides another operator "->" that has higher precedence than the "*". This allows struct pointers to access struct elements using the following syntax:
|