2017-12-02 84 views
-1

我定義在主函數的字符串數組,我想更新它的另一個函數內部如下:更新字符串數組用C

#include <stdio.h> 

#define SIZE 15 

void read_arrays(char *competitors[SIZE], float points[SIZE], int numOfCompetitors) 
{ 
     for (int cntr = 0; cntr < numOfCompetitors; cntr++) 
     { 
       printf("Enter the name of competitor %d", cntr+1); 
       scanf("%s", &*competitors[cntr]); 
       printf("Enter the point of competitor %d", cntr+1); 
       scanf("%f", &points[cntr]); 
     } 
} 

int main() 
{ 
     char *competitors[SIZE]; 
     float points[SIZE]; 
     int numOfCompetitors = 0; 
     while (numOfCompetitors > 15 || numOfCompetitors < 1) 
     { 
       printf("Enter the number of competitors: "); 
       scanf("%d", &numOfCompetitors); 
       if (numOfCompetitors > 15) printf("Number of competitors cannot be more than 15!\n"); 
     } 

     read_arrays(&*competitors[SIZE], &points[SIZE], numOfCompetitors); 
     printf("%f", points[0]); 

} 

,但我得到了以下錯誤:

cc  homework2.c -o homework2 
homework2.c: In function ‘main’: 
homework2.c:28:14: warning: passing argument 1 of ‘read_arrays’ from incompatible pointer type [-Wincompatible-pointer-types] 
    read_arrays(&*competitors[SIZE], &points[SIZE], numOfCompetitors); 
      ^
homework2.c:5:6: note: expected ‘char **’ but argument is of type ‘char *’ 
void read_arrays(char *competitors[SIZE], float points[SIZE], int numOfCompetitors) 

我想在循環中用scanf分配字符串數組中的值。我如何設法做到這一點?

+2

'read_arrays(competions,points,numOfCompetitors);'? – melpomene

+1

您尚未將內存分配給指針數組char *競爭者[SIZE];'的元素。因此'scanf'在scanf(「%s」,&*競爭者[cntr])中取得了一個不確定的指針;' - 應該是'scanf(「%s」,競爭者[cntr]);'無論如何,是一個指針數組。 –

回答

1

您可以在將它傳遞給函數時使用變量的名稱,還需要指定char矩陣的大小(〜字符串數組)。

所以這個:read_arrays(&*competitors[SIZE], &points[SIZE], numOfCompetitors);

變爲:read_arrays(competitors, points, numOfCompetitors);

全碼:

#include <stdio.h> 

#define SIZE 15 

void read_arrays(char competitors[SIZE][30], float points[SIZE], int numOfCompetitors) 
{ 
    for (int cntr = 0; cntr < numOfCompetitors; cntr++) 
    { 
     printf("Enter the name of competitor %d", cntr+1); 
     // We read up to 29 characters => no overflow as the size is up to 30 
     scanf("%29s", competitors[cntr]); 
     printf("Enter the point of competitor %d", cntr+1); 
     scanf("%f", &points[cntr]); 
    } 
} 

int main() 
{ 
    char competitors[SIZE][30]; 
    float points[SIZE]; 
    int numOfCompetitors = 0; 
    while (numOfCompetitors > 15 || numOfCompetitors < 1) 
    { 
     printf("Enter the number of competitors: "); 
     scanf("%d", &numOfCompetitors); 
     if (numOfCompetitors > 15) printf("Number of competitors cannot be more than 15!\n"); 
    } 

    read_arrays(competitors, points, numOfCompetitors); 

    printf("%s", competitors[0]); 
    printf("%s", competitors[1]); 
    printf("%f", points[0]); 
} 
+1

'*&'是毫無意義的。另外,'scanf''%s'是一個bug(緩衝區溢出)。 – melpomene

+0

代碼已更新,thx。 –

0

作爲一種替代丹尼爾ILLESCAS你可以只對每個競爭對手,你輸入分配空間。一定要稍後釋放它們。

#include <stdio.h> 
#define SIZE 15 

void read_arrays(char *competitors[SIZE], float points[SIZE], int numOfCompetitors) 
{ 
    for (int cntr = 0; cntr < numOfCompetitors; cntr++) 
    { 
     competitors[cntr] = (char*)calloc(1, 32); 
     printf("Enter the name of competitor %d", cntr + 1); 
     scanf("%s", competitors[cntr]); 
     printf("Enter the point of competitor %d", cntr + 1); 
     scanf("%f", &points[cntr]); 
    } 
} 

int main() 
{ 
    char *competitors[SIZE]; 
    float points[SIZE]; 
    int numOfCompetitors = 0; 
    while (numOfCompetitors > 15 || numOfCompetitors < 1) 
    { 
     printf("Enter the number of competitors: "); 
     scanf("%d", &numOfCompetitors); 
     if (numOfCompetitors > 15) printf("Number of competitors cannot be more than 15!\n"); 
    } 

    read_arrays(competitors, points, numOfCompetitors); 
    printf("%f", points[0]); 

}