简体   繁体   English

如何修复函数及其声明的“冲突类型”?

[英]How to fix “conflicting types” for a function and its declaration?

I want to pass a two-dimensional char array to a function but don't know how to declare the function before the main(). 我想将二维char数组传递给函数,但不知道如何在main()之前声明该函数。 The function compiles and works well before I declare it. 在我声明它之前,该函数可以编译并运行良好。 But after I declare it, I encounter compiling issues. 但是在声明之后,我遇到了编译问题。

I'm using EMACS on MacBook pro. 我在MacBook Pro上使用EMACS。 The compiler is gcc.I tried to declare my function print string various ways including 编译器是gcc。我试图通过各种方式声明函数打印字符串,包括

void printstring(int, int,char **);

or 要么

void printstring(int, int,char *); 

But none of them work. 但是它们都不起作用。 My Full codes are: 我的完整代码是:

#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;
}

I expected that there is no compiling error but I got one error and one warning. 我希望没有编译错误,但我收到一个错误和一个警告。 Details can be found below: 详细信息可以在下面找到:

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 **);
      ^~~~~~~~~~~

the problem is that you're using a variable length array. 问题是您使用的是可变长度数组。 The last argument (the list of strings) depends on the second argument ( m ). 最后一个参数(字符串列表)取决于第二个参数( m )。 And char ** is not suitable, as it's just a pointer on pointers. char **不适合,因为它只是指针上的指针。 So the max dimension of the strings would be lost when iterating on the 2D array. 因此,在2D数组上进行迭代时,字符串的最大尺寸将丢失。

Use a standard forward declaration, copying exactly the real declaration if you don't want to put the function before the main one. 使用标准的前向声明,如果您不想将函数放在主声明之前,则完全复制真实的声明。

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

If you have read-only strings, I suggest that you use a standard array of constant pointers instead: 如果您有只读字符串,建议您使用标准的常量指针数组代替:

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

note that 注意

printstring(3,word);

can be replaced by 可以替换为

printstring(sizeof(word)/sizeof(word[0]),word);

before array decays to pointer (that autocomputes the number of strings) 在数组衰减到指针之前(会自动计算字符串数)

The following should just work: 以下应该可以正常工作:

void printstring(int n, int m, char (*w)[m]);

The function prototype and definition should be kept identical, except maybe for certain qualifiers such as const and default arguments in C++. 函数原型和定义应保持相同,除了某些限定符(例如C ++中的const和默认参数)外。

If you want to maintain a name free declaration for whatever reason, you can use the * notation (reserved to function prototype scope) for the variably modified type 如果出于任何原因要保留无名称声明,则可以将*表示法(保留给函数原型作用域)用于可变类型

void printstring(int, int,char (*)[*]);

Still a VLA, and in fact, exactly equivalent to the notation that uses m . 仍然是VLA,实际上与使用m的符号完全等效。 Though, ostensibly, it may convey intent not as clearly as using m in the forward declaration. 尽管从表面上看,它传达的意图不如在前向声明中使用m清楚。

char** cannot be used to point at 2D arrays, it can only be used to point at the first element of a 1D array of char* , which is something else. char**不能用于指向2D数组,它只能用于指向char*的1D数组的第一个元素,这是其他内容。

Your compiler error is from having non-matching declaration and definition. 您的编译器错误来自于声明和定义不匹配。 Correct code: 正确的代码:

void printstring(int n, int m, char w[n][m]);

...

void printstring(int n, int m, char w[n][m]){
  ...
}

Alternatively, you can write void printstring(int n, int m, char (*w)[m]) and it is completely equivalent. 或者,您可以编写void printstring(int n, int m, char (*w)[m]) ,它是完全等效的。 But that is just harder to read, so why would you? 但这很难读,所以为什么呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM