包含数组定义的代码在 main 函数中运行良好,但当我从单独的文件中包含它时却不行,为什么?

标签 c arrays string include

所以我有这段代码,它构建了一个字符串数组(23 个不同大小的字符串)。 为此,我像这样分别定义了每个字符串:

char text1[] = "The content of string 1\n";
char text2[] = "The content of string 2 which is longer\n";

剩下的 21 个字符串也是如此。然后我为每个字符串定义了 23 个指向 char 的指针,如下所示:

char *ptext1 = text1; 
char *ptext2 = text2;

剩下的 21 个字符串也是如此。然后我定义了一个指向 char 的指针数组,并用我刚创建的 23 个指针对其进行了初始化:

char *parray[23];
parray[0] = ptext1;
parray[1] = ptext2; // ...etc.

现在,为了按索引访问字符串,我使用 for 循环遍历元素:

for (i = 0; i < 23; i++)
{
        printf("%s", parray[i]);
}

但是,当我将所有字符串、指针和数组定义移动到一个头文件时,这段代码在 main 函数中的工作就像一个魅力(因为我认为将定义放在一边是一个好习惯,对吧?)并将其包含在 main.c 文件中,其中我只有 for 循环,但它不起作用。编译器会显示各种警告和错误,例如:

warning: Data definition has to type or storage class (Then it shows this line:) parray[0] = text1;
error: Conflicting types for 'parray'
note: Previous declaration of 'parray' was here: char *parray[23];
error: Invalid Initialized parray[0] = text1;

它继续为所有 23 个元素显示类似的警告和错误。

那么为什么这段代码在 main 函数中按我想要的方式工作,但在我包含它时却不是这样?

注意:

我正在为 Windows 使用 mingw gcc 编译器,当我编译时,我使用这个命令:gcc main.c 头文件当然在同一个目录下。

这里是复制/过去或错误输出:

In file included from x.c:2:0:
x.h:15:2: warning: data definition has no type or storage class [enabled by default]
  parray[0] = ptext11;
  ^
x.h:15:2: error: conflicting types for 'parray'
x.h:13:8: note: previous declaration of 'parray' was here
  char *parray[2];
        ^
x.h:15:2: error: invalid initializer
  parray[0] = ptext11;
  ^
x.h:16:2: warning: data definition has no type or storage class [enabled by default]
  parray[1] = ptext12;
  ^
x.h:16:2: error: conflicting types for 'parray'
x.h:13:8: note: previous declaration of 'parray' was here
  char *parray[2];
        ^
x.h:16:2: error: invalid initializer
  parray[1] = ptext12;
  ^

这是 main.c 文件:

#include <stdio.h>
#include "main.h"


void main(void)
{

    int i;
    for (i = 0; i < 2; i++)
    {
        printf("%s", parray[i]);
    }
}

和 main.h 文件:

char text11[] = "Content of string 1";

char text12[] = "Content of string 2, which is longer";


char *ptext11 = text11, *ptext12 = text12;

char *parray[2];

parray[0] = ptext11;
parray[1] = ptext12;

最佳答案

在您的代码中,text1是单char .您想要的 char 数组中的内容

更改代码

char text1 = "The content of string 1\n";

char *parray[23];
parray[0] = ptext1;
parray[1] = ptext2; // ...etc.

char text1[] = "The content of string 1\n";
char *ptext1 = text1; 

char **parray= NULL;
parray = malloc(23 * sizeof (*parray));
parray[0] = ptext1;
parray[1] = ptext2; // ...etc.

最后,free(parray) .

注意:使用char *ptext1家庭是多余的。使用更正后的版本。你可以直接写类似 parray[0] = text1 的东西等等。


编辑:

根据您最近的编辑,您似乎正在尝试在头文件中一个接一个 初始化数组的每个元素。这是不允许的。将数组放在头文件中使数组的范围成为全局范围。您只能在声明/定义时使用 char *parray[23] = {<set of initializers>} 初始化全局数组,不是之后。你不能这样做

parray[0] = ptext1;
parray[1] = ptext2;

在头文件中[全局范围]。否则,您必须在函数内部执行此操作。

关于包含数组定义的代码在 main 函数中运行良好,但当我从单独的文件中包含它时却不行,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27646188/

相关文章:

java - Java 中 ArrayList 的合并排序

c# - 拆分字符串的最佳方法是什么

iphone - NSMutableString appendString 的内存泄漏

Javascript将字符串数组的数组转换为数字数组的数组

c - 防止 Keil uVision 中 bool 值的 "enumerated type mixed with another type"警告

c - 打印内存时出现段错误

c++ - 识别哪个文件包含了一些特定的头文件

c - 解析文件中的一行

arrays - 数组最后一个元素的简洁表示法

python - 使用数组输入神经网络