首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从C中头文件中的函数返回多维字符数组

如何从C中头文件中的函数返回多维字符数组
EN

Stack Overflow用户
提问于 2015-11-01 18:31:04
回答 3查看 83关注 0票数 1

我有一个主文件和一个头文件。

在主文件中,我希望从头文件的char函数返回一个2D char数组。我的char功能如下:

代码语言:javascript
复制
char character_distribution(int length, char redistribution[length][2])
{
        char *buffer, distribution[256][2] = {0};
        long lSize;
        struct Bar result = funct();
        buffer = result.x;
        lSize = result.y;
        length = collect_character_distribution(buffer, lSize, distribution);
        reorganize_character_distribution(length, distribution, redistribution);

        return redistribution;
}

我的主要职能如下:

代码语言:javascript
复制
#include <stdio.h>
#include "character_distribution.h"

void main()
{
        int length;
        char distribution[length][2];
        distribution = character_distribution(length, distribution[length][2]);
        int a;
        for(a = 0; a < length; a++)
        {
                printf("%c\n", distribution[a][0]);
        }
}

当我运行我的代码时,我会得到以下错误:

代码语言:javascript
复制
warning: return makes integer from pointer without a cast

我怎样才能解决这个问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-11-01 18:50:28

代码语言:javascript
复制
void character_distribution(int length, char redistribution[][2])
{
    char *buffer, distribution[256][2] = {0};
    long lSize;
    struct Bar result = funct();
    buffer = result.x;
    lSize = result.y;
    length = collect_character_distribution(buffer, lSize, distribution);
    reorganize_character_distribution(length, distribution, redistribution);
}
int main()
{
    int length = 2; //initialize
    char distribution[length][2];
    character_distribution(length, distribution);
    int a;
    for(a = 0; a < length; a++)
    {
        printf("%c\n", distribution[a][0]);
    }

    return 0;
}

如果您真的必须返回2d数组,一种方法(简单的方法)就是将其放入结构中。

代码语言:javascript
复制
struct distribution_struct {
    char x[256];
    char y[2];
};
struct distribution_struct character_distribution(int length, char redistribution[][2]) {
    struct distribution_struct dis;
    //initialize the struct with values 
    //return the struct 
}

另一种方法是手动为函数中的2d数组分配内存并返回它。

代码语言:javascript
复制
char** character_distribution(int length, char redistribution[][2]) {
     //use malloc to create the array and a for loop to populate it 
}
票数 1
EN

Stack Overflow用户

发布于 2015-11-01 19:37:41

实际上,不能从C函数返回数组。但是,您可以返回指向这样一个数组的指针。在这种情况下,正确的声明是:

代码语言:javascript
复制
char (*character_distribution(int length, char redistribution[][2]))[][2]

对初始维度进行大小调整并不是必要的,我怀疑,它实际上并不符合标准C(至少,按照您在问题中所做的那样,用length对其进行大小调整对我来说是可疑的)。这是因为数组是通过引用隐式传递的(在这种情况下,是通过引用显式返回的),并且不需要知道第一个维度,就可以计算已被赋予指向数组的指针(和索引)的元素的地址。

请注意,您不应该将指针返回到在本地为函数确定作用域的数组,因为一旦函数返回它的存储空间就会被释放(这样这样的指针就无效了)。

但是,您的问题表明,您实际上不需要返回数组。由于数组无论如何都是通过引用传递的,因此更改传入数组将导致调用方也可以看到的更改。您的代码可以编写为:

代码语言:javascript
复制
void character_distribution(int length, char redistribution[][2])
{
        char *buffer, distribution[256][2] = {0};
        long lSize;
        struct Bar result = funct();
        buffer = result.x;
        lSize = result.y;
        length = collect_character_distribution(buffer, lSize, distribution);
        reorganize_character_distribution(length, distribution, redistribution);
}

代码语言:javascript
复制
#include <stdio.h>
#include "character_distribution.h"

void main()
{
        int length = 256;   // you need to initialise this...
        char distribution[length][2];
        // No assignment needed here!:
        character_distribution(length, distribution /* [length][2] - remove this! */);
        int a;
        for(a = 0; a < length; a++)
        {
                printf("%c\n", distribution[a][0]);
        }
}

(当然,这依赖于您所称的其他各种函数的执行情况)。

票数 1
EN

Stack Overflow用户

发布于 2015-11-01 18:33:02

将签名更改为:

代码语言:javascript
复制
char** character_distribution(int length, char redistribution[length][2])

您正在返回一个multidimensional数组,而不是一个字符。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33465669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档