i'm supposed to prompt the user for the size of the array (number of strings, M) and allocate memory for it.
then have the user input strings assigning each one an integer from 1 to M.
my TA said that we basically have to compare the individual strings, but only swap the integers 1 through M in another array, so that in the end, i can print out the original array of strings, and then print out the sorted strings using the pointers from the other array.
and then i have to free up the memory again to reiterate the program.
i have the basic structure of the program w/o using the 2nd array of pointers.
the teacher wants us to use a structure like this:
Code:
typedef struct {
char * name;
int data;
} Data, *pData;
and the array of data should be declared like this:
here's what i have so far...
Code:
#include <stdio.h>
typedef struct {
char * name;
int data;
} Data, *pData;
pData pArray;
char s[100];
int main(void) {
int M, i, j, l;
printf("Size of array?\n->");
scanf("%d",&M);
pArray = (pData)malloc((M),sizeof(Data));
double a[M];
for (i = l, i < M, i++) {
printf("Enter string #%d: ", i);
s = gets();
l = strlen(s);
pArray[i].name = (char *)malloc(char l+i);
strncpy(pArray[i].name, s);
pArray[i].data = i;
}
qsort(a, M, sizeof(int), cmp);
for (j = 0; j < M; j++)
printf("\t%s\n", a[i].name);
exit(0);
}
int cmp(const void *Vp, const void *Vq)
{
int i, j;
i = (int) *Vp;
j = (int) *Vq;
strcmp(pArray[i].name, pArray[j].name);
}