#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sortString(char *s[], int count);
int main(){
int i;
char buff[BUFSIZ];
int count;
// 's' is a pointer to a char pointer, initially 's' is allocated storage for one char pointer
char** s= malloc(sizeof(char*));
printf("Here is the list of unsorted names: \n\n");
// Keep on reading unlimited number of names until EOF (Ctrl + D) is reached
for (count = 0; fgets(buff, sizeof(buff), stdin); count++){
//Step 1: allocate sufficient storage for s[n] to store the content in buff plus one byte for '\0'
s[count]=malloc(strlen(buff)+1);
//Step 2: copy the contents of 'buf' to 's[n]'
strcpy(s[count],buff);
//Step 3: resize the array of pointers pointed to by 's' to increase its size for the next pointer
*s=realloc(*s,sizeof(char **)*count+2);
printf("added to the array at s[%i] is : %s",count, s[count]);
}
// EOF reached. Now count the number of strings read
printf("\nCount is %d\n\n", count);
// Now sort string using sortString function
// Step 4: implement sortString function for the above-mentioned function declaration
//sortString(s, count);
// Step 5: print the list of sorted names.
int k;
for(k=0; k<count; k++){
printf("%s", s[k]);
}
// Step 6:free the allocated memory.
return 0;
}
void sortString(char *s[], int count){
char * temp;
int j,k;
for(j = 0; j < count -1; j++){
for(k=j+1; k < count; k++){
if(strncmp(s[k],s[k+1],1)>0){
temp=s[k];
s[k]=s[k+1];
s[k+1] = temp;
}
}
}
}
Full disclosure, this is a homework.
The idea is this : We have a pointer, s, that points to more char pointers, which are char array pointers themselves. We then malloc these pointers in order to fit a string the user enters, then realloc s to fit in more strings.
The malloc and realloc seems fine. I added a line to check what I've added at each array index, and it seems to work just fine.
The issue arises when I try to print it. I'm getting a segfault at step 5, specifically the printf statement. It looks as if s[k] will work for any positive integer before 4, because it segfaults when k is more than 4.
Compiled using gcc -g, ubuntu 18.04 and windows 10. All face the same issue.
sizeof(char **)*count+2-->sizeof(char *)*(count+2)- chux - Reinstate Monica