SE250:HTTB:Pointers:Arrays and pointers
<html>
<image src="http://www.rajithaonline.com/SE250/httb/pointers/final/arrays_head.png" width="434" height="92" alt="Arrays Header Logo" /> |
</html>
Previous Page | Contents | Next Page |
Array basics
An array is simply a collection of data of the same type. This collection has a single name and a fixed size, and each element in the collection is accessed by an index. The index starts from zero to one minus the size of the collection. i.e. index >= 0 and index < size.
In C, arrays are declared similarly to a normal variable. Except that the number of elements in the array is specified in square brackets after the variable name. Sample C code for making an array of integers of size four is shown below:
int array[4] = {24, 15, 2, 78};
This can be visualized as in this diagram:
<html> <img src="http://www.rajithaonline.com/SE250/httb/pointers/array1.png" width="557" height="231" alt="C Array in Memory" /> </html>
The size of the array (i.e. the number in the brackets) can also be an expression but it must be an expression that can be evaluated at compile time (and not run-time). e.g. the code below is WRONG:
void my_function( int x ) { int array[ x ]; } int main( void ) { my_function( 2 ); }
That code might actually work on GCC but won't on compilers which only support the standard ANSI C like the Visual Studio C/C++ Compiler. If you try the above code in the Visual Studio compiler you might get a message like this:
arrays.c arrays.c(7) : error C2057: expected constant expression arrays.c(7) : error C2466: cannot allocate an array of constant size 0 arrays.c(7) : error C2133: 'array' : unknown size
To use our array is very simple. Just put the name of the array and the index you want to access in square brackets:
int array[4]={0,1,2,3}; printf( "%d %d %d %d\n", array[0], array[1], array[2], array[3] );
Note that we used the index zero to access the first element, and index "n-1" (in this case three) to access the last element, where n = number of elements in the array.
The name of an array is actually a pointer. You can use it similar to a pointer but there are some differences:
- The sizeof keyword (when used on a array name) in C would return the total number of bytes in the array. Whereas if it was used on a pointer, it would return the number of bytes the pointer uses up in memory (usually 4 bytes in a 32-bit operating system)
- Unlike a normal pointer, you cannot make the array "pointer" (i.e. the name of the array) point to something else. e.g. You cannot do this:
int array[4]; int num; array = #
If we need to have a pointer that can point to different arrays we have to declare a pointer and make it point to that array:
int *p; int arr[4] = {0,1,2,3}; p = arr;
Note that we use the name of the array without an & (address-of) operator because the name of the array is evaluated to the address of the first element in the array at compile time. We can then use this pointer and make it point to another array at any time. e.g.
int means[4]; int medians[6]; int *p; p = means; /* use p in some way ... */ p = medians; /* use p again.... */
But there is a better way to deal with these situations, which you can read more about in the "Sizeof, Malloc and Free" chapter.
In short:
- Arrays are indexed collection of variables of the same type.
- They have a fixed size that is determined at compile time.
- The elements are accessed using an index which starts from zero to one minus the size of the array.
- The array name is actually a pointer to the first element of the array but it cannot be modified.
Using pointers with arrays
Arrays and pointers are like twins in C. Pointers can be used to do any operation involving array subscripting.
Assume integer array num[10] and integer pointer ptr are defined. Since the array name(without a subscript) is a pointer to the first element of the array, we can do this:
ptr = num;
which is equivalent to:
ptr = &num[0];
Element at position 8 can be referred by:
*(ptr + 8);
The 8 is an offset to the pointer. Offset value is identical to the array subscript. The parentheses are necessary as the precedence of * is higher than +, so if there are no brackets, the above would add 8 to the value of *ptr(that is, 8 would be added to num[0], assuming ptr points to the beginning of the array).
Just as array element can be referenced with a pointer expression:
&num[8];
can be written with pointer expression:
prt + 8;
The array itself can be treated as a pointer and used in pointer arithmetic. Eg.
*(num+8);
also refers to the array element[8]. Generally, all subscripted array expressions can be written with a pointer and an offset. In this case, pointer/offset notation was used with the name of the array as a pointer. Note that num still points to the beginning of the array, and does not increment by 8.
Arrays can also contain pointers. A common use for this is an array of strings.
Example of notation:
char *name[5] = {"John", "Mark", "Oliver", "Ian", "Richard"};
Even though the name array can only store 5 names, we can have strings of any lengths. The array of strings are stored in a 2-d array in which each row represents a name, and each column representing a letter.
Not Examined. Extra Notes I found in the book that I thought was interesting. There are 4 ways to refer to an array/pointer:
- Array Subscript Notation: num[10] = 10;
- Pointer/Offset Notation (pointer is array name): *(num+10) = 10;
- Pointer Subscript Notation: ptr[10] = 10;
- Pointer/Offset Notation: *(ptr+10) = 10;
Previous Page | Contents | Next Page |