0

Pointers to Pointers

It’s possible to create a pointer that holds the address of another pointer, called “a pointer to a pointer”.

Here is an example of a pointer to a pointer:

char **ptr;		// Declaration of a pointer to a pointer
char *ptr;	// Declaration of a normal pointer
char c;	// Normal variable declaration

	// Below is the usage of a pointer to a pointer
char c = 'a';
char *p_c = &c;
char **p_p_c = &p_c;

The usage of pointers to pointers is mostly on a function that takes a pointer parameter which needs to use that pointer to point to something else.

It’s also useful for working with multi-dimensional arrays. For example,

char *argv[];	// is the same as
char **argv;

It can also form an indefinite pointer to pointer, for example, a pointer to a pointer to a pointer to a pointer… However, this indefinite form is rarely used.

The following is the same code for pointers to pointers:

/*
 *
 * ptr2ptr.c
 *
 * Program to demonstrate the use of pointers to pointers
 *
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

struct employee {
	char name[31];
	char address[101];
	int age;
	float salary;
};

typedef struct employee emp;

/*
 *
 * Function to find the oldest of 2 emp structures
 * the pointer to a pointer to an emp "e_ptr" has
 * its contents (a pointer to an emp) assigned to the appropriate one
 *
 */
static void oldest(emp emps[], emp **e_ptr) {
	if (emps[0].age > emps[1].age) {
		*e_ptr = emps + 0;	// Note: it's *e_ptr
	}
	else {
		*e_ptr = &(emps[1]);
	}
}

static void goodbye() {
	printf("nPress ENTER to exit: ");
	fflush(stdin);
	getchar();
}

main() {
	emp emps[2] = {
		{"Sob", "123 Hope St", 45, 4500.00},
		{"Fred", "32 Nowhere Rd", 51, 49000.00}
	};
	emp *elder;

	atexit(goodbye);

	oldest(emps, &elder);

	printf("The oldest employee is aged %dn", elder->age);
}
0

C Arrays

An array is a variable that represents a collection of the same variable types. For instance, a collection of doubles is an array. It can also declare one variable as an array. The size of an array depend on the memory size of the target computer. It can store larger array size if the memory size is bigger.

Below is the arrays declaration format:

typename arrayname[init_literal];

As you remember from previous posts, this format is the same as when declaring strings. A string is a collection of characters, which also is an array.

After an array was declared, each element in the array can be accessed individually. For example:

floatarray[5] = 3.56;

Like other programming languages, the first element of an array in C always starts with 0. Therefore for the above example, floatarray[5] refers to the sixth element of the array.

Arrays are often used with loops to operate with all elements sequentially, especially the for loop. When used inside a loop, an array of n elements always stars from 0 to n – 1. For instance, an array of 10 elements would start from 0 to 9 inside a loop.

Here is the sample code for the arrays:

/*
 *
 * array.c
 *
 * Program to illustrate the use of arrays
 *
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>

main() {
 float array[5];
 int i, index;

 for (i = 0; i < 5; ++i) {
  printf("Please enter element number %d: ", i + 1);
  scanf("%f", &array[i]);
   while (getchar() != 'n' && getchar() != EOF)
    printf("");
 }

 printf("Which element do you want to see? ");
 scanf("%d", &index);
 while (getchar() != 'n' && getchar() != EOF)
  printf("");

 printf("Element %d is %fn", index, array[index-1]);

 fflush(stdin);
 getchar();
}

The following sample code demonstrates how to use strings with arrays:

/*
 *
 * string.c
 *
 * Program to illustrate the makeup of a string
 *
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>

main() {
 char name[51];
 int length;

 printf("Please enter your name: ");
 scanf("%[^n]", name);
 while (getchar() != 'n' && getchar() != EOF)
   printf("");

 for (length = 0; name[length] != ''; length++) {}    // do nothing

 printf("The length of >>>%s<<< is %dn", name, length);

 fflush(stdin);
 getchar();
}

Notice the name[length] != ” statement. The slash zero inside the quotes represents a NULL character. It means that the loop will end if there is no more character to read.

Multi-dimensional Arrays

A multi-dimensional Array is an array made of multiple arrays. Here is an example:

/*
 *
 * multi.c
 *
 * Program to demonstrate multi-dimensional arrays
 *
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>

main() {
 int grid[8][3];    // It contains 8 arrays and each array has 3 elements
 int i, j;

 for (i = 0; i < 8; i++) {
  for (j = 0; j < 3; j++) {
   grid[i][j] = (i + 1) * (j + 1);
   }
 }

 for (i = 0; i < 8; i++) {
  for (j = 0; j < 3; j++) {
   printf("%3d ", grid[i][j]);
  }
  printf("n");
 }

 fflush(stdin);
 getchar();
}

And here is the sample code for the string usage in a multi-dimensional array:

/*
 *
 * multi2.c
 *
 * Program to demonstrate multi-dimensional arrays
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>

main() {
 char strings[5][20];    // It contains 5 strings and each has 20 characters long
 int i;

 for (i = 0; i < 5; i++) {
  printf("Please enter string %d: ", i + 1);
  scanf("%s", strings[i]);
  while (getchar() != 'n' && getchar() != EOF) {}
 }

 for (i = 0; i < 5; i++) {
  printf("String %d is %sn", i + 1, strings[i]);
 }

 fflush(stdin);
 getchar();
}

Array Initialization

Arrays can be initialized at the beginning of the program when they are declared. During the initialization, it’s unneccessay to specify the size of the array. For instance, here is the declaration of an array:

int array[40];

And here is the initialization of an array when it is declared:

int array[] = {3, 6, 9, 12};

Note that it’s also possible to initialize the array when the size of the array is specified, but as stated above, it’s not necessary. For example:

int array[10] = {3, 6, 9, 12};

In the above example, the first four elements in the array got initialized, while the rest of the elements were set to 0.

Here is the sample code to show how the multi-dimensional arrays are initialized:

/*
 *
 * multi3.c
 *
 * Program to demonstrate initializing multi-dimensional arrays
 *
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>

main() {
 char grid[8][8] =
 {
  {'r', 'h', 'b', 'k', 'q', 'b', 'h', 'r'},
  {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
  {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
  {'r', 'h', 'b', 'k', 'q', 'b', 'h', 'r'}
 };
 int i, j;

 for (i = 0; i < 8; i++) {
  for (j = 0; j < 8; j++) {
   printf("%c", grid[i][j]);
  }
  printf("n");
 }

 fflush(stdin);
 getchar();
}

And here is for the multi-dimensional arrays for strings:

/*
 *
 * multi4.c
 *
 * Program to demonstrate multi-dimensional arrays
 *
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>

main() {
 char name[50] = "Mark Virtue";
 char strings[][20] = {"This", "is", "how", "to", "initialize"};
 int i;

 for (i = 0; i < 5; i++) {
  printf("Please enter string %d: ", i + 1);
  scanf("%s", strings[i]);
 }

 for (i = 0; i < 5; i++) {
  printf("String %d is %sn", i + 1, strings[i]);
 }

}

Note that strings in the multi-dimensional arrays can only have one set of arrays that has non-specified size, notably the arrays in the first dimension. The size of arrays in other dimensions needs to be specified.