如何在C中返回一个字符串数组?

时间:2021-05-31 19:40:11

For example, I have in the main file

例如,我在主文件中

1) char ** array[NUMBER];
2) array = build_array();

and in an imported file

并在导入的文件中

char ** build_array()
{
  char ** array[NUMBER];
  strings[0] = "A";
  strings[1] = "B";
  return (char *) strings;
}

However, at line 2 in the main file, I get the error: "incompatible types when assigning to type 'char **[(unsighed int)NUMBER]' from type 'char **'

但是,在主文件的第2行,我收到错误:“从类型'char **'分配类型'char ** [(unsighed int)NUMBER]时不兼容的类型'

What am I doing wrong? Any suggestions or advice would be appreciated. Thank you in advance.

我究竟做错了什么?任何建议或意见将不胜感激。先谢谢你。

3 个解决方案

#1


9  

There seem to be some confusion about what a string is in C. In C, a null terminated sequence of chars is considered a string. It is usually represented by char*.

关于C中的字符串是什么似乎有些混淆。在C中,空终止的字符序列被认为是字符串。它通常用char *表示。

I just want to call the build_array() function and return the array of strings

我只想调用build_array()函数并返回字符串数组

You pretty much can't return an array, neither a pointer to a local array. You could however pass the array to build_array as an argument, as well as its size, and fill that instead.

你几乎不能返回一个数组,也不能返回一个指向本地数组的指针。但是,您可以将数组作为参数传递给build_array,以及它的大小,并填充它。

void build_array( char* strings[], size_t size )
{
  // make sure size >= 2 here, based on your actual logic
  strings[0] = "A";
  strings[1] = "B";
}
...later called as:...
char *array[NUMBER];
build_array(array, NUMBER);

The alternatives are to return a pointer to a global or static allocated array, which would make your function non-reentrant. You probably don't care about this now, but is bad practice so I would recommend you avoid going that route.

替代方法是返回指向全局或静态分配数组的指针,这将使您的函数不可重入。你现在可能不关心这个,但这是不好的做法所以我建议你避免走那条路。

#2


2  

As littleadv pointed out, there are several problems with your code:

正如littleadv指出的那样,您的代码存在一些问题:

  • Mismatch between char ** and char **[ ]

    char **和char ** []之间不匹配

  • Returning a pointer to a local variable

    返回指向局部变量的指针

  • Etc.

This example might help:

此示例可能有所帮助:

#include <stdio.h>
#include <string.h>
#include <malloc.h>

#define NUMBER 2
#define MAX_STRING 80

char **
build_array ()
{
  int i = 0;
  char **array = malloc (sizeof (char *) * NUMBER);
  if (!array)
    return NULL;
  for (i = 0; i < NUMBER; i++) {
    array[i] = malloc (MAX_STRING + 1);
    if (!array[i]) {
      free (array);
      return NULL;
    }
  }
  strncpy (array[0], "ABC", MAX_STRING);
  strncpy (array[1], "123", MAX_STRING);
  return array;
}

int
main (int argc, char *argv[])
{
  char **my_array = build_array ();
  if (!my_array) {
    printf ("ERROR: Unable to allocate my_array!\n");
    return 1;
  }
  else {
    printf ("my_array[0]=%s, my_array[1]=%s.\n",
      my_array[0], my_array[1]);
  }
  return 0;
}

#3


1  

Your return type is char**, while you're assigning it to char**[], that's incompatible.

你的返回类型是char **,而你将它分配给char ** [],这是不兼容的。

Other than that you should post the actual code that you have problem with, the code you posted doesn't compile and doesn't make much sense.

除此之外,您应该发布您遇到问题的实际代码,您发布的代码不会编译,也没有多大意义。

In order to fix your code, the function should be returning char **[NUMBER]. Note also, that you're casting the return value to char* instead of char** that you declared (or char **[NUMBER] that it should be, and in fact - is).

为了修复你的代码,函数应该返回char ** [NUMBER]。另请注意,您将返回值转换为char *而不是您声明的char **(或char ** [NUMBER]它应该是,实际上是 - )。

Oh, and returning a pointer to a local variable, as you do in your case, is a perfect recipe for crashes and undefined behavior.

哦,并返回指向局部变量的指针,就像你的情况一样,是崩溃和未定义行为的完美配方。

What you probably meant was:

你的意思是:

char *array[NUMBER];
int ret = build_array(array, NUMBER);
// do something with return value or ignore it

and in an imported file

并在导入的文件中

int build_array(char **arr, int size)
{
  // check that the size is large enough, and that the
  // arr pointer is not null, use the return value to
  // signal errors
  arr[0] = "A";
  arr[1] = "B";
  return 0; // asume 0 is OK, use enums or defines for that
}

#1


9  

There seem to be some confusion about what a string is in C. In C, a null terminated sequence of chars is considered a string. It is usually represented by char*.

关于C中的字符串是什么似乎有些混淆。在C中,空终止的字符序列被认为是字符串。它通常用char *表示。

I just want to call the build_array() function and return the array of strings

我只想调用build_array()函数并返回字符串数组

You pretty much can't return an array, neither a pointer to a local array. You could however pass the array to build_array as an argument, as well as its size, and fill that instead.

你几乎不能返回一个数组,也不能返回一个指向本地数组的指针。但是,您可以将数组作为参数传递给build_array,以及它的大小,并填充它。

void build_array( char* strings[], size_t size )
{
  // make sure size >= 2 here, based on your actual logic
  strings[0] = "A";
  strings[1] = "B";
}
...later called as:...
char *array[NUMBER];
build_array(array, NUMBER);

The alternatives are to return a pointer to a global or static allocated array, which would make your function non-reentrant. You probably don't care about this now, but is bad practice so I would recommend you avoid going that route.

替代方法是返回指向全局或静态分配数组的指针,这将使您的函数不可重入。你现在可能不关心这个,但这是不好的做法所以我建议你避免走那条路。

#2


2  

As littleadv pointed out, there are several problems with your code:

正如littleadv指出的那样,您的代码存在一些问题:

  • Mismatch between char ** and char **[ ]

    char **和char ** []之间不匹配

  • Returning a pointer to a local variable

    返回指向局部变量的指针

  • Etc.

This example might help:

此示例可能有所帮助:

#include <stdio.h>
#include <string.h>
#include <malloc.h>

#define NUMBER 2
#define MAX_STRING 80

char **
build_array ()
{
  int i = 0;
  char **array = malloc (sizeof (char *) * NUMBER);
  if (!array)
    return NULL;
  for (i = 0; i < NUMBER; i++) {
    array[i] = malloc (MAX_STRING + 1);
    if (!array[i]) {
      free (array);
      return NULL;
    }
  }
  strncpy (array[0], "ABC", MAX_STRING);
  strncpy (array[1], "123", MAX_STRING);
  return array;
}

int
main (int argc, char *argv[])
{
  char **my_array = build_array ();
  if (!my_array) {
    printf ("ERROR: Unable to allocate my_array!\n");
    return 1;
  }
  else {
    printf ("my_array[0]=%s, my_array[1]=%s.\n",
      my_array[0], my_array[1]);
  }
  return 0;
}

#3


1  

Your return type is char**, while you're assigning it to char**[], that's incompatible.

你的返回类型是char **,而你将它分配给char ** [],这是不兼容的。

Other than that you should post the actual code that you have problem with, the code you posted doesn't compile and doesn't make much sense.

除此之外,您应该发布您遇到问题的实际代码,您发布的代码不会编译,也没有多大意义。

In order to fix your code, the function should be returning char **[NUMBER]. Note also, that you're casting the return value to char* instead of char** that you declared (or char **[NUMBER] that it should be, and in fact - is).

为了修复你的代码,函数应该返回char ** [NUMBER]。另请注意,您将返回值转换为char *而不是您声明的char **(或char ** [NUMBER]它应该是,实际上是 - )。

Oh, and returning a pointer to a local variable, as you do in your case, is a perfect recipe for crashes and undefined behavior.

哦,并返回指向局部变量的指针,就像你的情况一样,是崩溃和未定义行为的完美配方。

What you probably meant was:

你的意思是:

char *array[NUMBER];
int ret = build_array(array, NUMBER);
// do something with return value or ignore it

and in an imported file

并在导入的文件中

int build_array(char **arr, int size)
{
  // check that the size is large enough, and that the
  // arr pointer is not null, use the return value to
  // signal errors
  arr[0] = "A";
  arr[1] = "B";
  return 0; // asume 0 is OK, use enums or defines for that
}