Skip over navigation


Pointers and Basic Structures in C++

Other Data Structures



Linked Lists and the new Operator

A common way to use pointers with structs is to create a linked list. Start by defining a struct to hold a single list element:

struct link
	int val;
	link* next_link;	// pointer to another link struct

Now you want to be able to keep track of a space in memory without assigning a variable name to it. To do this, use the new operator with a pointer:

link* list = new link;	// reserves a space in memory for one link

Now you can assign information to this list element:

list->val = 3;

At this point, the other data member, list->next_link, is not pointing to anything. It should be set to NULL to indicate this. You could have it point to a new link by repeating the same process as above:

list->next_link = new link;
list->next_link->val = 5;

Now the linked list contains two links: one with val=3 and the other with val=5. Meanwhile list->next_link->next_link is invalid and should be set to NULL until you use it new to create another link. Thus, a NULL value in the next field indicates the end of the list.

Before a program terminates, you should dispose of dynamically allocated memory. All pointers on which you used the new operator should be released using the corresponding delete operator. The following code segment demonstrates one way to delete a linked list like the one above:

link* next_marked_link = list;		// a temp variable to keep track

// the following lines loop through the linked list until a NULL link is reached
for (link* marked_link = list; marked_link != NULL; marked_link = marked_link->next)
	delete marked_link;	// the delete function releases memory for you.


Arrays are a more basic way to store information. Suppose you want to keep track of the population of each of the 50 states in the US. You could create 50 variables, 1 per state and give each a different name, to keep track of the information. A far better way to do so would be to create an array:

long State_population[50];

This keeps all the information together, and makes it much easier to keep track of variables. You can then assign values to the array elements:

State_population[4] = 3;	// State #4 has 3 people

The number in brackets is called the index of the array. State_population[] has 50 elements, so you can use indices from 0 up to 49 to access its elements (arrays are "zero-indexed", meaning that the first spot is index zero, not index one). Trying to access State_population[50] would give an "array out of bounds" error and probably crash your program.

If you know all the values for an array at the time of declaration, you can define it immediately:

int Days_in_month[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Notice there is no number between the square brackets. This is an option, although for clarity's sake you can include it.

Arrays can have multiple dimensions. To create an array that represents a 3x3 matrix, you could type:

const int matrix_size = 3;
int matrix[matrix_size][matrix_size];
matrix[0][2] = 6;

Multidimensional arrays can also be initialized at declaration time:

char rect[3][2] = {	{'a', 'b'},
			{'c', 'd'},
			{'e', 'f'} };

Since arrays are essentially pointers (see the arrays SparkNote and the pointers SparkNote for more on this), they are passed by reference:

void func_name(int array[]);

This might later be called as follows:

int data[3] = {4, 1, 7};

Follow Us