【问题标题】:Returning Array of pointers to string返回指向字符串的指针数组
【发布时间】:2013-09-27 23:10:58
【问题描述】:

对于如何从函数返回指向字符串(基本上是二维数组)的指针数组,我感到困惑和无能。

我在这段代码中所做的(或即将做的)是,首先插入单词/名称,然后将其存储在数组中,并输入数字“n”。然后我将这个数组和数字传递给函数,然后他们从数组中提取了最后 'n' 个名称/单词并在 main 中打印出来。

这是我的代码:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
char** fun(char *s[5],int no)
{
    char *f[5];
    int i,j=0;
    for(i=5-no;i<5;i++)
    {
        f[j]=(char*)malloc(strlen(s[i])+1);
        strcpy(f[j],s[i]);
        j++;
    }
    /*for(j=0;j<no;j++)         //I did it just to check whether 'f' stores the last 'n' names.
    printf("\n%d. %s",j+1,f[j]);*/
    return f;
}
void main()
{
    char *name[5],*str,*list[5];
    int i,n;
    clrscr();
    printf("Enther the Names of the list : " );
    for(i=0;i<5;i++)
    {
        printf("%d. ",i+1);
        str=(char*)malloc(30);
        gets(str);
        name[i]=(char*)malloc(strlen(str)+1);
        strcpy(name[i],str);
        free(str);
    }
    clrscr();
    printf("\nEntered Names are : ");
    for(i=0;i<5;i++)
    printf("\n%d. %s",i+1,name[i]);
    printf("\n Enter the no. :");
    scanf("%d",&n);
    *list=*fun(name,n); // I am little confused as to how should I wrote this ?
    for(i=0;i<n;i++)
    printf("\n%d. %s",i+1,list[i]);
    getch();
}

假设我将输入作为:

1.the

2.there

3.this

4.that

5.therefore

输入编号:3

输出:

 1.this

 2.<Some special characters>

 3.<Some special characters>

我对使用指针方法的方法更感兴趣。 PS:我正在使用 Turbo C/C++ IDE,因为我处于 C 的学习阶段。

【问题讨论】:

  • 您不能返回本地自动数组,这是未定义的行为。看看malloc()(不要转换它的返回值),或者让你的函数填充一个作为参数传递给它的数组。
  • @H2CO3 即使我做 static char *f[5] ,我也会遇到同样的问题。有没有其他选择?顺便说一句,您提供的链接我觉得它完全不同,因为我强调解决此问题的指针方法。我希望我足够清楚
  • 除了staticmalloc()?不,没有。我可以向您保证,这很简单,如果您遇到错误,您应该在谷歌上搜索错误消息以了解其含义,然后相应地调整您的代码,直到它工作为止。
  • main 必须返回int

标签: c arrays string function pointers


【解决方案1】:

您不能在本地定义 char 指针数组(它将在堆栈上分配它。它的范围仅在该函数内部)并在全局范围内使用它。 在 fun() 你可以使用 char f = (char)malloc(sizeof(char*[5])); (在堆上分配内存,您可以全局使用它) 代替 字符 *f[5];

代码应该是:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include <malloc.h>
char** fun(char *s[5],int no)
{
    char **f = (char**)malloc(sizeof(char*[5]));
    int i,j=0;
    for(i=5-no;i<5;i++)
    {
        f[j]=(char*)malloc(strlen(s[i])+1);
        strcpy(f[j],s[i]);
        j++;
    }
    /*for(j=0;j<no;j++)         //I did it just to check whether 'f' stores the last 'n' names.
    printf("\n%d. %s",j+1,f[j]);*/
    return f;
}

    void main()
    {
        char *name[5],*str,**list;
        int i,n;

        printf("Enther the Names of the list : " );
        for(i=0;i<5;i++)
        {
            printf("%d. ",i+1);
            str=(char*)malloc(30);
            gets(str);
            name[i]=(char*)malloc(strlen(str)+1);
            strcpy(name[i],str);
            free(str);
        }

        printf("\nEntered Names are : ");
        for(i=0;i<5;i++)
            printf("\n%d. %s",i+1,name[i]);
        printf("\n Enter the no. :");
        scanf("%d",&n);
        list=fun(name,n); // I am little confused as to how should I wrote this ?
        for(i=0;i<n;i++)
            printf("\n%d. %s",i+1,list[i]);
        getch();
    }

【讨论】:

    【解决方案2】:

    正如H2CO3 所指出的,将fun 定义为fun(list, name, n);,其中list 是您的字符串输出列表。从main() 传递list,将其填入fun()

    【讨论】:

    • @user2805872 这个问题在 SO 上已经被问得太多了。我建议你看看副本。
    猜你喜欢
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多