0

The code is supposed to take in as many string as the user wants to put in until they enter EOF. and it is doing that but after i try to ouput the code it comes out with these little half boxes instead of the string.

void sortString(char *s[], int count);

int main(){

    int i;
    char buff[BUFSIZ];
    int count;
    char** s = (char**)malloc(sizeof(char*));

    //allows user to keep typing until EOF is reached.
    printf("Here is the list of unsorted names: \n\n");
    for (count = 0; fgets(buff, sizeof(buff), stdin); count++)
    {
        s[count] = malloc((sizeof(buff))*sizeof(char));//allocats memory at s[count].
        strcpy(buff, s[count]);//adds the string in buff to s[count].
        s = (char**) realloc(s, ((sizeof(s) + sizeof(buff)) * sizeof(char*)) + 1);//then reallocats memeory for s to take another string.
    }

    printf("\nCount is %d\n\n", count);
   // Now sort string using sortString function
   // Step 4: implement sortString function for the above-mentioned function declaration
   for (i = 0; i < count; i++){
            printf("%s \n",s[i]);
   }
   sortString(s, count);
   printf("Here is the list of sorted names: \n\n");
   for (i = 0; i < count; i++){
            printf("%s",s[i]);
   }

1 Answer 1

2

strcpy(buff, s[count]);//adds the string in buff to s[count].

No it doesn't. strcpy(dest, src), so it is copying s[count] (which is a buffer full of "random junk") to buff.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much I was driving my self up the wall looking for the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.