将 String 传递给 C 中的函数时出现类型冲突警告

发布于 2024-11-03 18:35:52 字数 663 浏览 0 评论 0原文

我已经有一段时间没有用 C 编程了,并且在将字符串传递给函数时遇到了问题。 该代码有效,但我收到来自 gcc 的警告。

我在 main 中调用该函数:

copyToCode(code, section1, section2);

该函数是:

void copyToCode (char **code, char *loc, char *data ){}

我在包含该函数的行上收到“copyToCode 的冲突类型”,并在调用该函数的行上收到“copyToCode 的先前隐式声明位于此处”警告。

我已经声明了变量:

char *code = malloc (32*1000* sizeof(char));

char *section1 = malloc(8*sizeof(char)), *section2 = malloc(8*sizeof(char));

我也尝试过: char *section1[8];

作为一个附带问题 - 哪个是正确的?

section1 和section2 是字符串,代码是字符串数组。

感谢您的阅读,我感谢任何帮助。 加雷思

I haven't programmed in C for awhile and having an issue with passing a string to a function.
The code works however I get warnings from gcc.

I call the function in my main with:

copyToCode(code, section1, section2);

The function is:

void copyToCode (char **code, char *loc, char *data ){}

I get "conflicting types for copyToCode" on the line containing the function and "previous implicit declaration of copyToCode was here" warning on the line calling the function.

I have declared the variables:

char *code = malloc (32*1000* sizeof(char));

char *section1 = malloc(8*sizeof(char)), *section2 = malloc(8*sizeof(char));

I also tried this :
char *section1[8];

As a side question - which is correct?

The section1 and section2 are meant to be Strings, and the code is meant to be an array of strings.

Thanks for reading, I appreciate any help.
Gareth

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

画▽骨i 2024-11-10 18:35:52

您需要在调用函数之前声明该函数,否则编译器将尝试代表您计算出函数原型是什么。

消息

之前的copyToCode隐式声明

告诉了你这一点。隐式声明是编译器因为您尚未给它显式声明而做出的声明。


在对问题的更新中,您说 code 旨在成为字符串数组,但您将其定义为:

char *code = malloc (32*1000* sizeof(char));

分配单个字符串。字符串数组将保存在 char** 中,就像 argv 一样。您需要首先分配数组,其中包含 n 个字符串,每个字符串都是 char*。然后你必须在循环中一一分配每个char*

在 C++ 中,使用标准库字符串和向量类进行此类编码要容易得多。

You need to declare the function before you call it, otherwise the compiler will try to work out what the function prototype is on your behalf.

The message

previous implicit declaration of copyToCode

is telling you this. An implicit declaration is one that the compiler makes because you haven't yet given it an explicit declaration.


In your update to the question you say that code is intended to be an array of strings but you define it as:

char *code = malloc (32*1000* sizeof(char));

That allocates a single string. An array of strings would be held in a char**, just like argv. You would need to allocate the array first, which would contain n strings, each being a char*. Then you'd have to allocate each char* one by one in a loop.

This sort of coding is so much easier in C++ with the standard library string and vector classes.

白馒头 2024-11-10 18:35:52

此警告意味着您的声明与实现不同。或者您有两个不同的函数声明。

因为警告提到了隐式声明,这意味着该声明是由 gcc 推导出来的,因为您在声明它之前使用了该函数。 GCC 将使用函数调用的参数来推导声明,这可能会导致严重的问题。

如果您声明该函数,您可能仍然会收到错误,但它应该更加具体。

旁注:如果您使用 gcc,请始终编译为 gcc -std=c99 -pedantic -Wall -Wextra -Wwrite-strings source.c

到您的编辑:变量是错误的。代码是 char * 但您的函数采用 char **

This warning means that you have your declaration differs from the implementations. Or you have two different declarations of the function.

Because the warning is mentioning an implicit declaration it means that the declaration was deduced by gcc because you used the function before declaring it. GCC will use the parameters of the function call to deduce the declaration, which can lead to nasty problems.

If you declare the function, you will probably still get an error, but it should be much more specific.

Side note: If you are working with gcc, always compile as gcc -std=c99 -pedantic -Wall -Wextra -Wwrite-strings source.c

To your edit: Your variables are wrong. code is char * but your function takes char **.

豆芽 2024-11-10 18:35:52

您能否为我们提供有关用作函数参数的变量的更多详细信息?

另外值得注意的是,copyToCode 的第一个参数是一个指向字符指针的指针,可以用作字符串数组代码>.

该行:

void copyToCode (char **code, char *loc, char *data ){}

是你的函数的声明吗?在这种情况下,你应该这样写:

void copyToCode (char **code, char *loc, char *data );

Can you give us more detail about the variables you are using as parameters of the function?

Also worth to note is the fact that the first parameter of copyToCode is a pointer to a pointer of chars, what can be used as an array of strings.

The line:

void copyToCode (char **code, char *loc, char *data ){}

is the declaration of your function? In that case, you should write it as this:

void copyToCode (char **code, char *loc, char *data );
痴情换悲伤 2024-11-10 18:35:52

要定义字符串数组,您需要类似以下内容:

char **code;
int i;
code = malloc (32*sizeof(*code));
for (i=0; i<32; i++) {
  code[i]=malloc(1000*sizeof(**code));
}

请注意,这不包括对 malloc 结果的错误检查,而您应该这样做。它创建了一个大小为 32 的数组,其中包含大小为 1000 的字符串,您不应该对其进行硬编码。

To define an array of strings, you'd need something like the following:

char **code;
int i;
code = malloc (32*sizeof(*code));
for (i=0; i<32; i++) {
  code[i]=malloc(1000*sizeof(**code));
}

Note that this doesn't include error checking on the malloc results, which you should do. And it creates an array of size 32 containing strings of size 1000, which you shouldn't be hard coding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文