Skip to content Skip to navigation

OpenStax-CNX

You are here: Home » Content » Pointers and Arrays

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • VOCW

    This module is included inLens: Vietnam OpenCourseWare's Lens
    By: Vietnam OpenCourseWare

    Click the "VOCW" link to see all content affiliated with them.

Recently Viewed

This feature requires Javascript to be enabled.
 

Pointers and Arrays

Module by: Huong Nguyen. E-mail the author

From the beginning, we only show how to access or change directly the values of variables through their names. However, the C language provides the developers an effective method to access variables - pointer.

A pointer is a variable that contains the address of a variable. Pointers are much used in C, partly because they are sometimes the only way to express a computation, and partly because they usually lead to more compact and efficient code than can be obtained in other ways. Pointers and arrays are closely related; this Unit also explores this relationship and shows how to exploit it.

Pointers and Addresses

Let us begin with a simplified picture of how memory is organized. A typical machine has an array of consecutively numbered or addressed memory cells that may be manipulated individually or in contiguous groups. One common situation is that any byte can be a char, a pair of one-byte cells can be treated as a short integer, and four adjacent bytes form a long.

Any variable in a program is stored at a specific area in memory. If you declare a variable, the compiler will allocate this variable to some consecutive memory cells to hold the value of the variable. The address of the variable is the address of the first memory cell.

One variable always has two properties:

  • The address of the variable
  • The value of the variable.

Consider the following Example

  int i, j;
  i = 3;
  j = i;

Type of these two variables is integer so they are stored in 2-byte memory area. Suppose that the compiler allocates i at the FFEC address in memory and j in FFEE, we have:

Table 1
Variable Address Value
i FFEC 3
j FFEE 3

Two different variables have different addresses. The i = j assignment affects only on the value of variables, that means the content of the memory area for j will be copied to the content of the memory area for i.

Pointers

A pointer is a group of cells (often two or four) that can hold an address. So if c is a char and p is a pointer that points to it, we could represent the situation this way:

Figure 1
Figure 1 (44.PNG)

Pointer declaration

If you declare a variable, its name is a direct reference to its value. If you have a pointer to a variable or any other object in memory, you have an indirect reference to its value. A pointer variable stores the address of another object or a function. To start out, the declaration of a pointer to an object that is not an array has the following syntax:

type * Name [= initializer];

In declarations, the asterisk (*) means "pointer to". The identifier name is declared as an object with the type *, or pointer to type. * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to.

Here is a simple Example

int *iPtr;          // Declare iPtr as a pointer to int.
double *realptr; // pointer to a double
char *astring; // pointer to a character

The type int is the type of object that the pointer iPtr can point to.

To make a pointer refer to a certain object, assign it the address of the object.

For example, if iVar is an int variable, then the following assignment makes iPtr point to the variable iVar:

iPtr = &iVar;       // Let iPtr point to the variable iVar.

In a pointer declaration, the asterisk (*) is part of an individual declarator. We can thus define and initialize the variables iVar and iPtr in one declaration, as follows:

int iVar = 77, *iPtr = &iVar; // Define an int variable and a
                              // pointer to it.

The second of these two declarations initializes the pointer iPtr with the address of the variable iVar, so that iPtr points to iVar. Figure 4.1. illustrates one possible arrangement of the variables iVar and iPtr in memory. The addresses shown are purely fictitious examples. As Figure 4.1. shows, the value stored in the pointer iPtr is the address of the object iVar.

Figure 2: A pointer and another object in memory
Figure 2 (45.PNG)

It is often useful to output addresses for verification and debugging purposes. The printf() functions provide a format specifier for pointers: %p. The following statement prints the address and contents of the variable iPtr:

printf("Value of iPtr (i.e. the address of iVar): %p\n"
        "Address of iPtr:                          %p\n", iPtr, &iPtr);

The size of a pointer in memory given by the expression sizeof(iPtr)

& and * operators

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 & operator only applies to objects in memory: variables and array elements. It cannot be applied to expressions, constants, or register variables.

* is the operator that retrieves the value stored at the address held in the pointer. The indirection operator * yields the location in memory whose address is stored in a pointer. If ptr is a pointer, then *ptr designates the object that ptr points to. Using the indirection operator is sometimes called dereferencing a pointer. The type of the pointer determines the type of object that is assumed to be at that location in memory. For example, when you access a given location using an int pointer, you read or write an object of type int.

The indirection operator * is a unary operator; that is, it has only one operand.ptr points to the variable x. Hence the expression *ptr is equivalent to the variable x itself.

Example 1
double x, y, *ptr;     // Two double variables and a pointer to double.
ptr = &x;              // Let ptr point to x.
*ptr = 7.8;            // Assign the value 7.8 to the variable x.
*ptr *= 2.5;           // Multiply x by 2.5.
y = *ptr + 0.5;      // Assign y the result of the addition x + 0.5.

Do not confuse the asterisk (*) in a pointer declaration with the indirection operator. The syntax of the declaration can be seen as an illustration of how to use the pointer.

double *ptr;

As declared here, ptr has the type double * (read: "pointer to double"). Hence the expression *ptr would have the type double.

Of course, the indirection operator * must be used with only a pointer that contains a valid address. This usage requires careful programming! Without the assignment ptr = &x in the listing above, all of the statements containing *ptr would be senseless dereferencing an undefined pointer value and might well cause the program to crash.

Pointer Assignment

Since pointers are variables, they can be used without dereferencing. Pointer assignment between two pointers makes them point to the same pointee. So the assignment iq = ip; copies the contents of ip into iq, thus making iq point to whatever ip pointed to. It makes iq point to the same pointee as ip. Pointer assignment does not touch the pointees. It just changes one pointer to have the same reference as another pointer. After pointer assignment, the two pointers are said to be "sharing" the pointee.

Example Consider the following programs:

main()
{
  int i = 3, j = 6;
  int *p1, *p2;
  p1 = &i;
  p2 = &j;
  *p1 = *p2;
}
and
main()
{
  int i = 3, j = 6;
  int *p1, *p2;
  p1 = &i;
  p2 = &j;
  p1 = p2;
}

Suppose the values of variables before executing the last assignment are

Table 2
Variable Address Value
i FFEC 3
j FFEE 6
p1 FFDA FFEC
p2 FFDC FFEE

After the assignment *p1 = *p2; for the first program:

Table 3
Variable Address Value
i FFEC 6
j FFEE 6
p1 FFDA FFEC
p2 FFDC FFEE

While the assignment p1 = p2 for the second program results

Table 4
Variable Address Value
i FFEC 3
j FFEE 6
p1 FFDA FFEE
p2 FFDC FFEE

Initializing Pointers

Pointer variables with automatic storage duration start with an undefined value, unless their declaration contains an explicit initializer. You can initialize a pointer with the following kinds of initializers:

  • A null pointer constant.
  • A pointer to the same type, or to a less qualified version of the same type.
  • A void pointer, if the pointer being initialized is not a function pointer. Here again, the pointer being initialized can be a pointer to a more qualified type.

Operators on Pointers

Besides using assignments to make a pointer refer to a given object or function, you can also modify an object pointer using arithmetic operations. When you perform pointer arithmetic, the compiler automatically adapts the operation to the size of the objects referred to by the pointer type.

You can perform the following operations on pointers to objects:

  • Adding an integer to, or subtracting an integer from, a pointer.
  • Subtracting one pointer from another.
  • Comparing two pointers.

If ip points to the integer x, then *ip can occur in any context where x could, so

*ip = *ip + 10; 

The unary operators * and & bind more tightly than arithmetic operators, so the assignment

y = *ip + 1 

takes whatever ip points at, adds 1, and assigns the result to y, while

*ip += 1 

increments what ip points to, as do

++*ip 

and

(*ip)++ 

The parentheses are necessary in this last example; without them, the expression would increment ip instead of what it points to, because unary operators like * and ++ associate right to left.

When you subtract one pointer from another, the two pointers must have the same basic type, although you can disregard any type. Furthermore, you may compare any pointer with a null pointer constant using the equality operators (== and !=), and you may compare any object pointer with a pointer to void.

Pointer to pointer

A pointer variable is itself an object in memory, which means that a pointer can point to it. To declare a pointer to a pointer , you must use two asterisks, as in the following Example

char c = 'A', *cPtr = &c, **cPtrPtr = &cPtr;

The expression *cPtrPtr now yields the char pointer cPtr, and the value of **cPtrPtr is the char variable c. The diagram in Figure X illustrates these references.

NULL Pointers

There are times when it’s necessary to have a pointer that doesn’t point to anything. A null pointer is what results when you convert a null pointer constant to a pointer type. A null pointer constant is an integer constant expression with the value 0, or such an expression cast as the type void *.

Null pointers are implicitly converted to other pointer types as necessary for assignment operations, or for comparisons using == or !=. Hence no cast operator is necessary in the previous example.

void Pointers

A pointer to void, or void pointer for short, is a pointer with the type void *. As there are no objects with the type void, the type void * is used as the all-purpose pointer type. In other words, a void pointer can represent the address of any object but not its type. To access an object in memory, you must always convert a void pointer into an appropriate object pointer.

Arrays

Basic of Arrays

An array contains objects of a given type, stored consecutively in a continuous memory block.The individual objects are called the elements of an array. The elements' type can be any object type. No other types are permissible: array elements may not have a function type or an incomplete type.

An array is also an object itself, and its type is derived from its elements' type. More specifically, an array's type is determined by the type and number of elements in the array. If an array's elements have type T, then the array is called an "array of T." If the elements have type int, for example, then the array's type is "array of int." The type is an incomplete type, however, unless it also specifies the number of elements. If an array of int has 16 elements, then it has a complete object type, which is "array of 16 int elements."

In C, there is a strong relationship between pointers and arrays, strong enough that pointers and arrays should be discussed simultaneously. Any operation that can be achieved by array subscripting can also be done with pointers. The pointer version will in general be faster but, at least to the uninitiated, somewhat harder to understand.

Declarations and Usage of Arrays

The definition of an array determines its name, the type of its elements, and the number of elements in the array. The general syntax for declaring a single-dimensional array is

type name[ number_of_elements ];

The number of elements, between square brackets ([ ]), must be an integer expression whose value is greater than zero.

For example, the declaration,

int a[10]; 

defines an array of size 10, that is, a block of 10 consecutive objects named a[0], a[1], ...,a[9].

char buffer[4*512];

defines an array with the name buffer, which consists of 2,048 elements of type char.

  • The lower bound of an array is set at 0. C++ does not allow you to override or alter this lower bound
  • Declaring a C++ array entails specifying the number of members. The number of member is equal to the upper bound plus one
  • The valid range of indices extends between 0 and number_of_elements -1.

Multidimensional Arrays

A multidimensional array in C is merely an array whose elements are themselves arrays. The elements of an n-dimensional array are (n-1)-dimensional arrays. For example, each element of a two-dimensional array is a one-dimensional array. The elements of a one-dimensional array, of course, do not have an array type.

A multidimensional array declaration has a pair of brackets for each dimension:

char screen[10][40][80];      // A three-dimensional array.

The array screen consists of the 10 elements screen[0] to screen[9]. Each of these elements is a two-dimensional array, consisting in turn of 40 one-dimensional arrays of 80 characters each. All in all, the array screen contains 32,000 elements with the type char.

Two-dimensional arrays are also called matrices. Because they are so frequently used, they merit a closer look. It is often helpful to think of the elements of a matrix as being arranged in rows and columns. Thus the matrix mat in the following definition has three rows and five columns:

float mat[3][5];

The three elements mat[0], mat[1], and mat[2] are the rows of the matrix mat. Each of these rows is an array of five float elements. Thus the matrix contains a total of 3 x 5 = 15 float elements, as the following diagram illustrates:

Table 5
  0 1 2 3 4
mat[0] 0.0 0.1 0.2 0.3 0.4
mat[1] 1.0 1.1 1.2 1.3 1.4
mat[2] 2.0 2.1 2.2 2.3 2.4

Accessing Array Elements

The subscript operator [ ] provides an easy way to address the individual elements of an array by index. If myArray is the name of an one dimensional array and i is an integer, then the expression myArray[i] designates the array element with the index i. Array elements are indexed beginning with 0. Thus, if len is the number of elements in an array, the last element of the array has the index len-1.

The following code fragment defines the array myArray and assigns a value to each element.


#define A_SIZE 4
long myarray[A_SIZE];
for (int i = 0;  i < A_SIZE;  ++i)
   myarray[i] = 2 * i;

The diagram in Figure 3 illustrates the result of this assignment loop.

Figure 3: Values assigned to elements by index
Figure 3 (35.PNG)

To access a char element in the three-dimensional array screen, you must specify three indices. For example, the following statement writes the character Z in a char element of the array:


screen[9][39][79] = 'Z';

Initializing Arrays

If you do not explicitly initialize an array variable, the usual rules apply: if the array has automatic storage duration, then its elements have undefined values. Otherwise, all elements are initialized by default to the value 0.

  • You cannot include an initialization in the definition of a variable-length array.
  • If the array has static storage duration, then the array initializers must be constant expressions. If the array has automatic storage duration, then you can use variables in its initializers.
  • You may omit the length of the array in its definition if you supply an initialization list. The array's length is then determined by the index of the last array element for which the list contains an initializer. For example, the definition of the array a in the previous example is equivalent to this:
    int a[ ] = { 1, 2, 4, 8 };     // An array with four elements.
    
  • If the definition of an array contains both a length specification and an initialization list, then the length is that specified by the expression between the square brackets. Any elements for which there is no initializer in the list are initialized to zero (or NULL, for pointers). If the list contains more initializers than the array has elements, the superfluous initializers are simply ignored.
  • A superfluous comma after the last initializer is also ignored.
  • As a result of these rules, all of the following definitions are equivalent:
    int a[4] = {1, 2};
    int a[ ]  = {1, 2, 0, 0};
    int a[ ]  = {1, 2, 0, 0, };
    int a[4] = {1, 2, 0, 0, 5};
    

Operations on arrays

Read the elements of a 1-dimensional array:

float a[10];	// declare a float array of size 10
int i;
// read the second element of the array : a[1]
scanf(“%f”,&a[1]);
// Assign an expression  to the third element of the array
a[2] = a[1] + 5;

To read the value for each element of an array, you should use for statement. For example,


int b[10];
int i;
// Read the value for each element of the array
for(i = 0; i < 10; i++)
{
	printf(“\n Enter the value of b[%d]”, i);
	scanf(“%d”,&b[i]);
}

In case you do not now the exact number of elements, declare the maximum number of elements and use a variable to store the actual size of the array


int a[100];	// Declare the array with the number of elements not greater than 100
	int n;			// n is the actual size of the array
	int i;
	printf(“\n Enter the number of elements: “);
	scanf(“%d”,&n);
	for(i = 0; i < n; i++)
	{
		printf("\n a[%d] = ", i);
		scanf("%d",&a[i]);
	}


C allow you to associate initializers with specific elements . To specify a certain element to initialize, place its index in square brackets. In other words, the general form of an element designator for array elements is:


int a[4] = {4, 9, 22, 16};
float b[3] = {40.5, 20.1, 100};
char c[5] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’};

The first statement is equivalent to four assign statements


a[0] = 4; a[1] = 9; a[2] = 22; a[3] = 16;

Printing array elements

printf function are used to print the element of an array. In the following example, we print the element of array a in different ways


#include <stdio.h>
#include <conio.h>
void main()
{
	int a[5];
	int i, k;
	// Read the elements of the array
	for(i = 0; i < 5; i++)
	{
		printf(“\n a[%d] = “, i);
		scanf(“%d”, &a[i]);
	}
	// print the value of element a[3] 
	printf(“\n a[3] = %d”, a[3]);
	// Display all the elements of array a, each element in a line.
	for(i = 0; i < 5; i++)
		printf(“\n%d”, a[i]);
	// Display all the elements of array a in a line
	printf(“\n”);	// change to a new line
	for(i = 0; i < 5; i++)
		printf(“%d  “, a[i]);
	// Display all the elements of array a, k elements in a line
	printf(“\n Enter the value of k = “);
	scanf(“%d”,&k);
	for(i = 0; i < 5; i++)
	{
		printf(“%d  “,a[i]);
		if((i+1)%k == 0)	// change to a new line after printing k     
                              //elements
			printf(“\n”);
	}
	getch();
}


here is the sample session with the above program


 a[0] = 6
 a[1] = 14
 a[2] = 23
 a[3] = 37
 a[4] = 9
 a[3] = 37
 6
 14
 23
 37
 9
 6  14  23  37  9
 Input the value of k = 2
 6  14
 23  37
 9


Find the maximum value stored in the array.

The purpose of this function is to find the maximum value stored in the array

  • Set up a trial minimum value. The function begins by declaring a variable named min and initializing that variable with a trial minimum value – value of the first element .
  • Then the function uses a while loop to:
    • Fetch the value stored in each element in the array
    • Compare each of those values with the current value stored in the variable named max
    • Possibly replace if the value fetched from an element is algebraically greater than the current value stored in max:
    • The value fetched from the element is stored in the variable named max
    • Replacing the value that was previously stored in the variable named max by the new value from the element.
  • When all of the array elements have been examined and processed in this manner, the variable named max will contain the maximum value of all the values stored in the array.

int a[100];
int i, n;
int max;
printf("\n Enter the size of the array: ");
scanf("%d",&n);
// Read the number of elements of the array
for(i = 0; i < n; i++)
{
	printf("\n a[%d] = ",i);
	scanf("%d",&a[i]);
}
// Find the maximum element
max = a[0];	// max is initialized by a[0]
// compare max to other elements
for(i = 1; i < n; i++)
	if(max < a[i])		//meet an element greater than max
		max = a[i];	// replace max by the new value from the elements.
printf("\n The maximum element of the array is: %d", max);

Searching

The simplest type of searching process is the sequential search. In the sequential search, each element of the array is compared to the key, in the order it appears in the array, until the first element matching the key is found. If you are looking for an element that is near the front of the array, the sequential search will find it quickly. The more data that must be searched, the longer it will take to find the data that matches the key using this process.

here is the sample session with the above program


#include <stdio.h>
#include <conio.h>
void main()
{
	int m[100], idx[100];
	int n;	// n is the actual size of the array
	int i, k, test;
	clrscr();	// clear screen
	// Read array m
	// Read the actual size of m
	printf(“ Enter the number of elements
	scanf(“%d”,&n);
	// Read array’s elements
for(i  = 0;i<n;i++)
	{
		int temp;
	printf(“\n Enter the value of m[%d] = “,i);
		scanf(“%d”,&temp);
		m[i] = temp;
	}
	// Read the searching key k
	printf(“\n Enter the value you want to search : “);
	scanf(“%d”,&k);
	// Begin searching
	test = 0;
	// Scan all the elements
	for(i = 0;i<n;i++)
		if(m[i] = = k)//Compare the current element with the           
                          //searching key k
		{
			// save the index of the current element
			idx[test] = i;
test ++; 
		}
	// Conclusion
	if(test > 0)
	{
		printf(“\n there are %d elements which has the value of %d”,test,k);
		printf(“\n Indexes of those elements: “);
		for(i = 0;i < test;i++)
			printf(“%3d”,idx[i]);
	}
	else
		printf(“\n No element has the value %d”,k);
	getch();	// Wait until the user press any key
}

Sorting

Selection sort is a sorting algorithm, specifically an in-place comparison sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations. It works as follows:

  • Find the minimum value in the list
  • Swap it with the value in the first position
  • Repeat the steps above for remainder of the list (starting at the second position)

Effectively, we divide the list into two parts: the sublist of items already sorted, which we build up from left to right and is found at the beginning, and the sublist of items remaining to be sorted, occupying the remainder of the array.

Here is an example of this sort algorithm sorting five elements:

31 25 12 22 11
11 25 12 22 31
11 12 25 22 31
11 12 22 25 31

#include <stdio.h>
#include <conio.h>
void main()
{
	int m[100];//100 is the maximum size for array m
	int n;	// n is the number of elements	int i, j, k;
	clrscr();	// clear screen 
	// Read the elements of array m
	// Read the actual size of the array

	printf(“ Enter the number of elements: “);
	scanf(“%d”,&n);
	// Read array elements 
for(i  = 0;i<n;i++)
	{
		int temp;
	printf(“\n Enter the value of m[%d] = “,i);
		scanf(“%d”,&temp);
		m[i] = temp;
	}
	// Print the array 
	printf(“\n The array before sorting:    “);
	for(i=0;i<n;i++)
		printf(“%3d”,m[i]);
	// Begin to sort
	for(i = 0; i<n-1;i++)
	{
		// Put the minimum value in the list of n-i elements
           //to the ith position 
	for(j = i+1;j<n;j++)
		{
			// compare m[i] with other element of the sublist
// and swap m[i] and m[j] if m[j] < m[i].
			if(m[j]<m[i])
			{
				int temp;
				temp = m[j]; m[j] = m[i]; m[i] = temp;
			}
		}
		// Print the array after the i+1 th step of sorting process
printf(“\n The array after step %d”,i+1);
		for(k = 0;k < n ;k++)
printf(“%3d”,m[k]);
	}
	getch();	// Wait until the user press any key.
}


here is the sample session with the above program

Enter the number of elements:  : 5
Enter the value of m[0]: 34
Enter the value of m[1]: 20
Enter the value of m[2]: 17
Enter the value of m[3]: 65
Enter the value of m[4]: 21
The array before sorting:    34 20 17 65 21
The array after step 1: 17 34 20 65 21
The array after step 2: 17 20 34 65 21
The array after step 3: 17 20 21 65 34
The array after step 4: 17 20 21 34 65

Pointers vs Arrays

Pointers occur in many C programs as references to arrays , and also as elements of arrays. A pointer to an array type is called an array pointer for short, and an array whose elements are pointers is called a pointer array.

Array Pointers

For the sake of example, the following description deals with an array of int. The same principles apply for any other array type, including multidimensional arrays.

To declare a pointer to an array type, you must use parentheses, as the following example illustrates:

int (* arrPtr)[10] = NULL; // A pointer to an array of
                           // ten elements with type int.

Without the parentheses, the declaration int * arrPtr[10]; would define arrPtr as an array of 10 pointers to int. Arrays of pointers are described in the next section.

In the example, the pointer to an array of 10 int elements is initialized with NULL. However, if we assign it the address of an appropriate array, then the expression *arrPtr yields the array, and (*arrPtr)[i] yields the array element with the index i. According to the rules for the subscript operator, the expression (*arrPtr)[i] is equivalent to *((*arrPtr)+i). Hence **arrPtr yields the first element of the array, with the index 0.

In order to demonstrate a few operations with the array pointer arrPtr, the following example uses it to address some elements of a two-dimensional array that is, some rows of a matrix:

int matrix[3][10];       // Array of three rows, each with 10 columns.
                         // The array name is a pointer to the first
                         // element; i.e., the first row.
arrPtr = matrix;         // Let arrPtr point to the first row of
                         // the matrix.
(*arrPtr)[0] = 5;  // Assign the value 5 to the first element of the
                   // first row.
                   //
arrPtr[2][9] = 6;   // Assign the value 6 to the last element of the
                    // last row.
                    //
++arrPtr;                // Advance the pointer to the next row.
(*arrPtr)[0] = 7;  // Assign the value 7 to the first element of the
                         // second row.

After the initial assignment, arrPtr points to the first row of the matrix, just as the array name matrix does. At this point you can use arrPtr in the same way as matrix to access the elements. For example, the assignment (*arrPtr)[0] = 5 is equivalent to arrPtr[0][0] = 5 or matrix[0][0] = 5.

However, unlike the array name matrix, the pointer name arrPtr does not represent a constant address, as the operation ++arrPtr shows. The increment operation increases the address stored in an array pointer by the size of one array in this case, one row of the matrix, or ten times the number of bytes in an int element.

If you want to pass a multidimensional array to a function, you must declare the corresponding function parameter as a pointer to an array type.

One more word of caution: if a is an array of ten int elements, then you cannot make the pointer from the previous example, arrPtr, point to the array a by this assignment:

arrPtr = a;    // Error: mismatched pointer types.

The reason is that an array name, such as a, is implicitly converted into a pointer to the array's first element, not a pointer to the whole array. The pointer to int is not implicitly converted into a pointer to an array of int. The assignment in the example requires an explicit type conversion, specifying the target type int (*)[10] in the cast operator:

arrPtr = (int (*)[10])a;     // OK

You can derive this notation for the array pointer type from the declaration of arrPtr by removing the identifier. However, for more readable and more flexible code, it is a good idea to define a simpler name for the type using typedef:

typedef int ARRAY_t[10];     // A type name for "array of ten int elements".
ARRAY_t a,                   // An array of this type,
        *arrPtr;             // and a pointer to this array type.
arrPtr = (ARRAY_t *)a;       // Let arrPtr point to a.

Pointer Arrays

Pointer arrays that is, arrays whose elements have a pointer type are often a handy alternative to two-dimensional arrays. Usually the pointers in such an array point to dynamically allocated memory blocks.

For example, if you need to process strings, you could store them in a two-dimensional array whose row size is large enough to hold the longest string that can occur:

#define ARRAY_LEN 100
#define STRLEN_MAX 256
char myStrings[ARRAY_LEN][STRLEN_MAX] =
{ // Several corollaries of Murphy's Law:
  "If anything can go wrong, it will.",
  "Nothing is foolproof, because fools are so ingenious.",
  "Every solution breeds new problems."
};

However, this technique wastes memory, as only a small fraction of the 25,600 bytes devoted to the array is actually used. For one thing, a short string leaves most of a row empty; for another, memory is reserved for whole rows that may never be used. A simple solution in such cases is to use an array of pointers that reference the objects in this case, the strings and to allocate memory only for the pointer array and for objects that actually exist. Unused array elements are null pointers.

#define ARRAY_LEN 100
char *myStrPtr[ARRAY_LEN] =    // Array of pointers to char
{ // Several corollaries of Murphy's Law:
  "If anything can go wrong, it will.",
  "Nothing is foolproof, because fools are so ingenious.",
  "Every solution breeds new problems."
};
Figure 4: Pointer array
Figure 4 (55.PNG)

The diagram in illustrates how the objects are stored in memory. The pointers not yet used can be made to point to other strings at runtime. The necessary storage can be reserved dynamically in the usual way. The memory can also be released when it is no longer needed.

Content actions

Download module as:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks