2017-03-01 6 views
0

コードは、EOFに入るまで、ユーザーが入れたい文字列を取り込むことになっています。それをやっているが、コードを出そうとすると文字列の代わりにこれらの小さな半ボックスが出てくる。strcpyを使用して配列の入力が間違っています

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

INTメイン(){

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]); 
    } 

答えて

2

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

いいえそれはありません。 strcpy(dest, src)ですので、buffにはs[count]( "ランダムな迷惑メール"で満ちているバッファ)がコピーされています。

+0

本当にありがとうございました。私は自分の問題を探して壁を上っていました。 –

関連する問題