如何在 ANSI C 程序中返回字符串数组?

发布于 2024-09-15 08:08:27 字数 327 浏览 11 评论 0原文

如何在 ANSI C 程序中返回字符串数组?

例如:

#include<stdio.h>

#define SIZE 10

char ** ReturnStringArray()
{
    //How to do this?
}

main()
{
    int i=0;

    //How to do here???

    char str ** = ReturnStringArray();

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

How can I return an array of strings in an ANSI C program?

For example:

#include<stdio.h>

#define SIZE 10

char ** ReturnStringArray()
{
    //How to do this?
}

main()
{
    int i=0;

    //How to do here???

    char str ** = ReturnStringArray();

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

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

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

发布评论

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

评论(3

桃气十足 2024-09-22 08:08:27

您可以执行以下操作。为简洁起见,省略了错误检查

char** ReturnStringArray() {
  char** pArray = (char**)malloc(sizeof(char*)*SIZE);
  int i = 0;
  for ( i = 0; i < SIZE; i++ ) {
    pArray[i] = strdup("a string");
  }
  return pArray;
}

请注意,您需要相应地释放返回的内存。

此外,在 printf 调用中,您可能需要在字符串中包含 \n 以确保刷新缓冲区。否则,字符串将排队并且不会立即打印到控制台。

char** str = ReturnStringArray();
for(i=0 ; i<SIZE ; i++)
{
    printf("%s\n", str[i]);
}

You could do the following. Error checking omitted for brevity

char** ReturnStringArray() {
  char** pArray = (char**)malloc(sizeof(char*)*SIZE);
  int i = 0;
  for ( i = 0; i < SIZE; i++ ) {
    pArray[i] = strdup("a string");
  }
  return pArray;
}

Note that you'd need to correspondingly free the returned memory.

Additionally in your printf call you'll likely want to include a \n in your string to ensure the buffer is flushed. Otherwise the strings will get queued and won't be immediately printed to the console.

char** str = ReturnStringArray();
for(i=0 ; i<SIZE ; i++)
{
    printf("%s\n", str[i]);
}
坐在坟头思考人生 2024-09-22 08:08:27

这样做

#include<stdio.h>

#define SIZE 10

char ** ReturnStringArray()
{
    //How to do this?
    char **strList = (char **)malloc(sizeof(char*) * SIZE);
    int i = 0;
    if (strList != NULL){
         for (i = 0; i < SIZE; i++){
             strList[i] = (char *)malloc(SIZE * sizeof(char) + 1);
             if (strList[i] != NULL){
                sprintf(strList[i], "Foo%d", i);
             }
         }
    }
    return strList;
}

main()
{
    int i=0;

    //How to do here???

    char **str = ReturnStringArray();

    for(i=0 ; i<SIZE ; i++)
    {
        printf("%s", str[i]);
    }
}
  • 问题 1:您的双指针声明不正确
  • 问题 2:您需要知道双指针中每个指针的字符串大小。
  • 问题 3:您有责任在完成后释放内存上面

的代码示例假设字符串的最大大小不会超过 SIZE 的值,即长度为 10 个字节...

不要超出双指针的边界,如它会崩溃

Do it this way

#include<stdio.h>

#define SIZE 10

char ** ReturnStringArray()
{
    //How to do this?
    char **strList = (char **)malloc(sizeof(char*) * SIZE);
    int i = 0;
    if (strList != NULL){
         for (i = 0; i < SIZE; i++){
             strList[i] = (char *)malloc(SIZE * sizeof(char) + 1);
             if (strList[i] != NULL){
                sprintf(strList[i], "Foo%d", i);
             }
         }
    }
    return strList;
}

main()
{
    int i=0;

    //How to do here???

    char **str = ReturnStringArray();

    for(i=0 ; i<SIZE ; i++)
    {
        printf("%s", str[i]);
    }
}
  • Problem 1: Your double pointer declaration was incorrect
  • Problem 2: You need to know the size of the string for each pointer in the double-pointer..
  • Problem 3: The onus is placed on you to free the memory when done with it..

The code sample above assumes that the maximum size of the string will not exceed the value of SIZE, i.e. 10 bytes in length...

Do not go beyond the boundary of the double pointer as it will crash

楠木可依 2024-09-22 08:08:27

请不要对 malloc 的返回进行类型转换,您还没有包含 并且正如上面有人指出的,缺少原型将导致 int 被转换为 char **。意外地,您的程序可能会也可能根本无法运行。

pls dont typecast the return of malloc, you have not included <stdlib.h> and as someone pointed out above lack of prototype will result in int being casted to char **. Accidently your program may or may not work at all.

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