Array of Pointers to Strings

The declaration of an array of character pointers is an extremely useful extension to single string pointer declarations. In the example below, A two element array of character pointers where each element is a pointer to a character. We have initialized the array with two elements by giving index numbers. In C, format specifier %s is used with the printf to print the string.

Sorting an array of strings requires a lot of swapping the strings which can require copying a lot of data. For efficiency, it is better to avoid swapping of data whenever a data item is large, such as a string or an entire data base record. In addition, arrays may be needed in more than one order; for example, we may need an exam scores array sorted by Id number and by weighted scores; or, we may need strings in both an unsorted form and a sorted form. In either of these cases, we must either keep two copies of the data, each sorted differently, or find a more efficient way to store the data structure. The solution is to use pointers to elements of the array and swap pointers. Consider some examples:

int data1, data2, *ptr1, *ptr2, *save;

data1 = 100; data2 = 200;

ptr1 = &data1; ptr2 = &data2;

We could swap the values of the data and store the swapped values in data1 and data2 or we could simply swap the values of the pointers:

save = ptr1;

ptr1 = ptr2;

ptr2 = save;

We have not changed the values in data1 and data2; but ptr1 now accesses data2 and ptr2 access data1. We have swapped the pointer values so they point to objects in a different order. We can apply the same idea to strings:

char name1[] = "John";

char name2[] = "Dave";

char *p1, *p2, *save;

p1 = name1;

p2 = name2;

Pointers p1 and p2 point to strings name1 and name2. We can now swap the pointer values so p1 and p2 point to name2 and name1, respectively.

In general, an array of pointers can be used to point to an array of data items with each element of the pointer array pointing to an element of the data array. Data items can be accessed either directly in the data array, or indirectly by dereferencing the elements of the pointer array. The advantage of a pointer array is that the pointers can be reordered in any manner without moving the data items. For example, the pointer array can be reordered so that the successive elements of the pointer array point to data items in sorted order without moving the data items. Reordering pointers is relatively fast compared to reordering large data items such as data records or strings. This approach saves a lot of time, with the additional advantage that the data items remain available in the original order.

As we know a pointer variable always contains an address therefore if we construct an array of pointers it would contain number of address.

In the two dimensional array of characters the strings occupy 60 bytes but in array of pointers the strings only occupy 41 bytes. Therefore we save a total of 19 bytes.

Actually 19 bytes are not saved, since 12 bytes are to be given for storing the addresses in the array names[]. Thus one more reason to store strings in an array of pointers is to make a more efficient use of available memory

Another reason to use an array of pointers to store strings is to obtain greater ease in manipulation of the strings.

Email-based homework help and assignment help at Transtutors.com offers wonderful insights and simulations which help make the subject practical and pertinent. Our tutors at Transtutors.com are extremely qualified and have years of experience providing Computer science homework help and assignment help.

Please send your problems related to Array of pointer to string and we will forward it to our tutors for analysis. We will provide you homework help and assignment help without plagiarism in a timely manner at reasonable price without compromising the quality