So you're talking about standard C, not C++
Yea I'll give you an example to show how it is done.. But you gotta understand what is happening, if it turns out this is an assignment I've just done for you, then I'm gonna need a hell of alot more from your part the next time.
Code:
#include <stdio.h> /* printf() */
#include <string.h> /* strlen(), strcpy() */
#include <stdlib.h> /* malloc(), realloc(), free() */
int main()
{
int i=0,j=0,size=0;
/* the list to sort, could've been
* created any way you like..
*/
char *str_arr[] = {"Jonny", "Janie", "Billy", "Georg",
"David", "Kelly", "Brian", "Steve",
"Allan", "Sammy", "Doris", "Bobby", NULL};
/* this is the actual array that's beeing sortet */
char** str_array;
char *tmp1, *tmp2;
/* make room for our array to be sortet */
size=sizeof(str_arr)/sizeof(*str_arr);
str_array = (char**)malloc (size*sizeof (char*));
/* copy everything into our array */
for(i=0;str_arr[i];++i)
{
str_array[i] = (char*) malloc(strlen(str_arr[i])*sizeof(char));
strcpy(str_array[i], str_arr[i]);
}
/* NULL terminate the array */
str_array[i] = NULL;
/* Verify theres atleast four (4) items in the end to shift */
size=i-4;
if(size > 0)
{
printf("Array befor shifting operations:");
for(i=0;str_array[i];++i)
printf("%c%d) %s", (i%4?'\t':'\n'), (i+1), str_array[i]);
printf("\n");
/* shift last four items to the front */
for(i=0;str_array[i];++i,++j)
{
/*
* For efficiency we use the last four places in the
* array over and over again to simulate a O(N) runtime
*
* The theory is this:
*
* Last four items should be placed as first four items
* 1) swap fourth last item with first item from pointer
* 2) swap third last item with second item from pointer
* 3) swap second last item with third item from pointer
* 4) swap last item with fourth item from pointer
* 6) repeat 1 - 4 untill either
* a) Pointer is placed where swap is placed (hence b)
* b) Theres no more items to swap
*/
if(!(i%4))
j=size; /* keep swap pointer to last 4 locations */
if(i>=j)
break; /* break if current pointer is at swap pointer */
/* make room for our swap */
tmp1 = (char*) malloc(sizeof(char)*strlen(str_array[i]));
tmp2 = (char*) malloc(sizeof(char)*strlen(str_array[j]));
/* swap what's at current location and swap pointer */
strcpy(tmp1, str_array[i]);
strcpy(tmp2, str_array[j]);
/* since our str_array is allocated, and we have no idear
* if the swap item is larger than what's previusly here
* then reallocate the memory
*/
str_array[i] = (char*) realloc(str_array[i],
sizeof(char)*strlen(tmp1));
str_array[j] = (char*) realloc(str_array[j],
sizeof(char)*strlen(tmp2));
/* swap the items back into our array */
strcpy(str_array[i], tmp2);
strcpy(str_array[j], tmp1);
/* We better make 'nice' with the system and
* give back what we've currently borrowed
*/
free(tmp1);
free(tmp2);
}
printf("\nArray after shifting operations:");
for(i=0;str_array[i];++i)
printf("%c%d) %s", (i%4?'\t':'\n'), (i+1), str_array[i]);
printf("\n");
}
else
printf("Error: There should be atleast 4 items in the list\n");
/* free everything up, that we've used so far */
for(i=0;str_array[i];++i)
free(str_array[i]);
free(str_array);
return 0;
}