我想将一个二维字符数组传递给一个函数,但不知道如何在 main() 之前声明该函数。该函数在我声明它之前编译并运行良好。但是在我声明它之后,我遇到了编译问题。
我在 MacBook pro 上使用 EMACS。编译器是 gcc。我尝试以各种方式声明我的函数打印字符串,包括
void printstring(int, int,char **);
或
void printstring(int, int,char *);
但是它们都不起作用。我的完整代码是:
#include<stdio.h>
#include<stdlib.h>
void printstring(int, int,char **);
int main(){
char word[3][6]= {"hello","world","I"};
printstring(3,6,word);
return 0;
}
void printstring(int n, int m, char (*w)[m]){
for (int i = 0; i < n; i++){
printf("%s\n",w[i]);
}
return;
}
我预计没有编译错误,但我得到了一个错误和一个警告。详情如下:
test.c: In function 'main':
test.c:9:19: warning: passing argument 3 of 'printstring' from incompatible pointer type [-Wincompatible-pointer-types]
printstring(3,6,word);
^~~~
test.c:5:6: note: expected 'char **' but argument is of type 'char (*)[6]'
void printstring(int, int,char **);
^~~~~~~~~~~
test.c: At top level:
test.c:13:6: error: conflicting types for 'printstring'
void printstring(int n, int m, char (*w)[m]){
^~~~~~~~~~~
test.c:5:6: note: previous declaration of 'printstring' was here
void printstring(int, int,char **);
^~~~~~~~~~~
最佳答案
问题是您使用的是可变长度数组。最后一个参数(字符串列表)取决于第二个参数(m
)。而 char **
不合适,因为它只是指针上的指针。因此,在二维数组上迭代时,字符串的最大维度将丢失。
使用标准的前向声明,如果您不想将函数放在主函数之前,请完全复制真实的声明。
void printstring(int n, int m, char (*w)[m]);
int main(){
char word[3][6]= {"hello","world","I"};
printstring(3,6,word);
return 0;
}
void printstring(int n, int m, char (*w)[m]){
for (int i = 0; i < n; i++){
printf("%s\n",w[i]);
}
return;
}
如果您有只读字符串,我建议您改用标准常量指针数组:
void printstring(int n, const char *w[]);
int main(){
const char *word[] = {"hello","world","I"};
printstring(3,word);
return 0;
}
void printstring(int n, const char *w[])
{
for (int i = 0; i < n; i++){
printf("%s\n",w[i]);
}
return;
}
注意
printstring(3,word);
可以替换为
printstring(sizeof(word)/sizeof(word[0]),word);
在数组衰减到指针之前(自动计算字符串的数量)
关于c - 如何修复函数及其声明的 "conflicting types"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57407759/