将二维数组传递给函数
Passing 2 dimensional array to function
我正在尝试使用 c
#include <stdio.h>
int getWordAmount(FILE *file);
void getWords(char names[][]);
int main() {
FILE *file = fopen("files/country.txt", "a+");
if(file != NULL) printf("File is opened\n");
else printf("Error occured in opening file\n");
int n = getWordAmount(file);
char names[n][20];
getWords(names);
for(int i = 0; i < n; i++) {
for(int j = 0; names[i][j] != '[=10=]'; j++) {
printf("%c", names[i][j]);
}
}
return 0;
}
int getWordAmount(FILE *file) {
char c;
int charAmount = 0;
while ((c = getc(file)) != EOF) {
if((c == ' ')||(c == '\n')) charAmount++;
}
return charAmount;
}
void getWords(char names[][]) {
int i = 0, j = 0, k = 0;
char temp[20];
while ((c = getc(file)) != EOF) {
temp[i++] = c;
if((c == ' ')||(c == '\n')) {
i = 0;
while((temp[i] != ' ')||temp[i] != '\n') {
names[j][k++] = temp[i++];
}
j++;
i = 0;
}
}
}
我收到如下所示的错误
E:\Programming\C Files\files.c:3:20: error: array type has incomplete
element type 'char[]' 3 | void getWords(char names[][]); | ^~~
E:\Programming\C Files\files.c:3:20: note: declaration of 'names' as
multidimensional array must have bounds for all dimensions except the
first E:\Programming\C Files\files.c: In function 'main':
E:\Programming\C Files\files.c:11:12: error: type of formal parameter
1 is incomplete 11 | getWords(names); | ^ E:\Programming\C
Files\files.c: At top level: E:\Programming\C Files\files.c:30:20:
error: array type has incomplete element type 'char[]' 30 | void
getWords(char names[][]){ | ^ E:\Programming\C Files\files.c:30:20:
note: declaration of 'names' as multidimensional array must have
bounds for all dimensions except the first E:\Programming\C
Files\files.c: In function 'getWords': E:\Programming\C
Files\files.c:33:11: error: 'c' undeclared (first use in this
function) 33 | while ((c = getc(file)) != EOF){ | ^ E:\Programming\C
Files\files.c:33:11: note: each undeclared identifier is reported only
once for each function it appears in E:\Programming\C
Files\files.c:33:20: error: 'file' undeclared (first use in this
function); did you mean 'fileno'? 33 | while ((c = getc(file)) !=
EOF){ | ^~ | fileno
这是我的country.txt
Bangladesh India China Nepal Bhutan
Pakistan Indonesia America Miyanmar
USA North-Korea South-korea Brazil
Argentina
Indonesia Japan Singapur Africa Rassia
正如您所看到的错误,多维数组的声明必须对除第一个维度之外的所有维度都有边界。
所以你可以这样做(你的问题的方法之一和最简单的方法):
// in your case the 20 is second dimension
void getWords(char names[][20]) {
int i = 0, j = 0, k = 0;
char temp[20];
while ((c = getc(file)) != EOF) {
temp[i++] = c;
if((c == ' ')||(c == '\n')) {
i = 0;
while((temp[i] != ' ')||temp[i] != '\n') {
names[j][k++] = temp[i++];
}
j++;
i = 0;
}
}
}
我正在尝试使用 c
#include <stdio.h>
int getWordAmount(FILE *file);
void getWords(char names[][]);
int main() {
FILE *file = fopen("files/country.txt", "a+");
if(file != NULL) printf("File is opened\n");
else printf("Error occured in opening file\n");
int n = getWordAmount(file);
char names[n][20];
getWords(names);
for(int i = 0; i < n; i++) {
for(int j = 0; names[i][j] != '[=10=]'; j++) {
printf("%c", names[i][j]);
}
}
return 0;
}
int getWordAmount(FILE *file) {
char c;
int charAmount = 0;
while ((c = getc(file)) != EOF) {
if((c == ' ')||(c == '\n')) charAmount++;
}
return charAmount;
}
void getWords(char names[][]) {
int i = 0, j = 0, k = 0;
char temp[20];
while ((c = getc(file)) != EOF) {
temp[i++] = c;
if((c == ' ')||(c == '\n')) {
i = 0;
while((temp[i] != ' ')||temp[i] != '\n') {
names[j][k++] = temp[i++];
}
j++;
i = 0;
}
}
}
我收到如下所示的错误
E:\Programming\C Files\files.c:3:20: error: array type has incomplete element type 'char[]' 3 | void getWords(char names[][]); | ^~~ E:\Programming\C Files\files.c:3:20: note: declaration of 'names' as multidimensional array must have bounds for all dimensions except the first E:\Programming\C Files\files.c: In function 'main': E:\Programming\C Files\files.c:11:12: error: type of formal parameter 1 is incomplete 11 | getWords(names); | ^ E:\Programming\C Files\files.c: At top level: E:\Programming\C Files\files.c:30:20: error: array type has incomplete element type 'char[]' 30 | void getWords(char names[][]){ | ^ E:\Programming\C Files\files.c:30:20: note: declaration of 'names' as multidimensional array must have bounds for all dimensions except the first E:\Programming\C Files\files.c: In function 'getWords': E:\Programming\C Files\files.c:33:11: error: 'c' undeclared (first use in this function) 33 | while ((c = getc(file)) != EOF){ | ^ E:\Programming\C Files\files.c:33:11: note: each undeclared identifier is reported only once for each function it appears in E:\Programming\C Files\files.c:33:20: error: 'file' undeclared (first use in this function); did you mean 'fileno'? 33 | while ((c = getc(file)) != EOF){ | ^~ | fileno
这是我的country.txt
Bangladesh India China Nepal Bhutan
Pakistan Indonesia America Miyanmar
USA North-Korea South-korea Brazil
Argentina
Indonesia Japan Singapur Africa Rassia
正如您所看到的错误,多维数组的声明必须对除第一个维度之外的所有维度都有边界。 所以你可以这样做(你的问题的方法之一和最简单的方法):
// in your case the 20 is second dimension
void getWords(char names[][20]) {
int i = 0, j = 0, k = 0;
char temp[20];
while ((c = getc(file)) != EOF) {
temp[i++] = c;
if((c == ' ')||(c == '\n')) {
i = 0;
while((temp[i] != ' ')||temp[i] != '\n') {
names[j][k++] = temp[i++];
}
j++;
i = 0;
}
}
}