Array of pointers in C

Did you ever have a need to store strings in a string arrays and not waste spaces?  In this post i will be explaining some of the ways that you can save yourself from destroying the RAM!

What are string arrays?
String arrays are basically arrays of strings where for example you have “morning”, “afternoon”, and “evening” in an array.  A string array is different from a char array (we are talking in C language here).

What is a two-dimentional string array?
Imagine you have a table of 7 by 8 and you have seven values which are “bashful”, “doc”, “dopey”, “grumpy”, “happy”, “sneezy” and “sleepy”.

The similar code for this declaration would be

char seven[7][8] = {"bashful", "doc", "dopey", "grumpy", "happy", "sneezy", "sleepy"};

As you can see, this declaration has wasted spaces due to the fixed size.

What is a string pointer array?
By using a pointer that points to an array of variable constants, this method saves memory as well as allow for more room of improvement during run-time.  By using a similar variable to the above, the representation would look like below.

By using a pointer, you erase the empty spaces that is not utilized.  Although the representation here saves 11 bytes of memory, in a larger picture you may see that conserving memory is an important task.

The similar code for this declaration would be

const char *seven[] = {"bashful", "doc", "dopey", "grumpy", "happy", "sneezy", "sleepy"};

It is recommended that you define the variable as a constant to avoid some common boo-boos (according to Dan Gookin).

The latter…

Basically, above is some of the concept you have to understand.  By using char array as displayed in the first image, you can see that it is much hefty.  The second method that has been explained teaches you to save memory.  The method that has been mention only helps you when you code them, what if you want them to be done run-time?  Here is a way to do it during run-time.

Let us assume you have created a program that requires the user to enter 7 strings.  We can create the table during run-time as below.

char **seven;
char names[17]; // this is a variable we use to store the names temporarily
int rowNum = 7; // we must know the number of rows our array have

seven = (char **)calloc(rowNum + 1, sizeof(char *));

for(int i = 0 ; i < rowNum ; i++)
	// store a name into a temporary variable
	scanf("%16[^\n]", names);

	// create the array
	// as you can see, we use strlen(names) +1 for the length of the current array index
	// +1 followed by strlen is because of the null terminator, \0
	seven[i] = (char *)calloc(strlen(names) + 1, sizeof(char));
	strncpy(seven[i], names, 16);

// do not allow the pointer to point to any random address
seven[rowNum] = NULL;


C All-in-one Desk Reference For Dummies by Dan Gookin.

Computer Science A Structured Programming Approach Using C Second Edition by Behrouz A. Forouzan and Richard F. Gilberg


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: