包含数组定义的代码在主函数中工作得很好,但是当我从一个单独的文件中包含它时却不能,为什么?

西西

所以我有这段代码,它构建了一个字符串数组(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.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个元素,它仍然显示相似的警告和错误。

那么,为什么这段代码可以像我希望在主函数中那样工作,而当我包含它时却不能工作呢?

笔记:

我在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;
ALK

在C语句中,例如:

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

可能不会出现在上下文外部,而只能出现在函数内部。

要初始化parray定义,请执行以下操作:

char *parray[2] = {ptext11, ptext12};

更新:

假设这个

char text11[] = "Content of string 1";
char text12[] = "Content of string 2, which is longer";
char *ptext11 = text11, *ptext12 = text12;

该代码甚至可以缩短为:

char *parray[2] = 
{
  "Content of string 1",
  "Content of string 2, which is longer"
};

或更方便的版本:

char *parray[] = 
{
  "Content of string 1",
  "Content of string 2, which is longer",
  NULL
};

所述NULL指示数组的末尾,并且可以使用,以确定在运行时间的元素的数量。这使您可以在较新版本的代码中向数组添加更多字符串,而无需调整定义数组大小的任何变量。

确定元素数量的函数示例为:

ssize_t number_of_array_elements(char ** parray)
{
  ssize_t result = 0;

  if (NULL == *parray)
  {
    result = -1;
    errno = EINVAL;
  }
  else
  {
    while (NULL != *parray)
    {
      ++result;
      ++parray;
    }
  }

  return result;
}

像这样使用它:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>


char * parray[] = {
  "Content of string 1",
  "Content of string 2, which is longer",
  NULL
};

int main(void)
{
  size_t size = 0;

  {
    ssize_t result = number_of_array_elements(parray);
    if (-1 == result)
    {
      perror("array_elements() failed");
      exit(EXIT_FAILURE);
    }

    size = result;
  }

  printf("parray holds %zu elements.\n", size);

  exit(EXIT_SUCCESS);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章