【问题标题】:Using malloc to return an int array in a function使用 malloc 在函数中返回一个 int 数组
【发布时间】:2021-03-07 15:04:27
【问题描述】:

我正在编写一个接收数字(十进制、十六进制或八进制)并将其转换为二进制补码形式的 C 程序。

该算法应该是正确的,因为我以前在其他地方使用过它,但我对如何返回 0 和 1 的 int 数组感到困惑。

我的理解是我必须使用 malloc 为这个新数组分配内存空间,但我不知道如何在语法上这样做。

这是我目前所拥有的:

char *itob (int num, int size) {
        int binary[size];
        binary[size] = (int *)malloc(size);
        for (int i=0; i<size; i++) {
                binary[i] = 0;
        }
        int decimal = num;
        int counter = 0;

        if (decimal > -32768 && decimal < 32767) {
                if (decimal < 0) {
                        decimal = 65536 + decimal;
                }
                while (decimal>0) {
                        binary[counter] = decimal%2;
                        decimal = decimal/2;
                        counter++;
                }
        }

        return binary;
}

参数num是要转换的数字,size是我要打印的位数。

【问题讨论】:

  • 1) 永远不要将 malloc 结果转换为 C。编译器会为您完成。这也可以帮助您正确处理:2) malloc 结果是一个指针,将其分配给指针变量:int *binary = malloc(sizeof(int)*size); 3) 使您返回的类型与您的 binary 变量类型相同。
  • 我尝试了int *binary = malloc(sizeof(int)*size); 并收到错误variable-sized object may not be initialized?
  • 删除int binary[size];这一行
  • 嗯..我做到了
  • 删除行'binary[size] = (int *)malloc(size);'。变量二进制有自动存储

标签: c malloc twos-complement


【解决方案1】:

如果你想分配并返回一个int变量数组,你需要你的函数返回一个int*值(不是char*);然后,在该函数内部简单地分配一个本地 int* 指针并在计算并分配其元素后返回它。

请注意,要分配的内存块的大小应该是元素的数量(在您的情况下是 size 参数)乘以每个元素的大小(这将是 @ 987654327@,用于您的代码)。

您可以在返回的指针上使用[](索引)运算符访问已分配数据中的元素,就像这是一个数组一样。

int* itob(int num, int size) // Returns an int array, not a char array
{
//  int binary[size];
//  binary[size] = (int*)malloc(size);
    int* binary = malloc(size * sizeof(int)); // Note: Don't cast the "malloc" return!
    for (int i = 0; i < size; i++) {
        binary[i] = 0;
    }
    int decimal = num;
    int counter = size - 1; // Your code will return the digits in reverse order!

    if (decimal > -32768 && decimal < 32767) {
        if (decimal < 0) {
            decimal = 65536 + decimal;
        }
        while (decimal > 0) {
            binary[counter] = decimal % 2;
            decimal = decimal / 2;
            counter--; // See the comment on the initialization of "counter"
        }
    }

    return binary;
}

不要忘记通过在 itob 返回的指针上调用 free() 来释放分配的内存(在调用模块中)。

关于 malloc 的转换(或不转换)返回(在 C 中),请参阅:Do I cast the result of malloc?

【讨论】:

  • 哇,谢谢!我想知道,如果我希望它返回字符,我将如何更改代码?我刚刚意识到我的头文件说要返回一个 char 数组,但我不确定这怎么可能,因为我正在做整数计算?你能指出我正确的方向吗?
  • @Sarah 阅读我的评论的第 2 点)。返回类型必须与变量binary 的类型匹配。如果您需要该返回类型,请更改 binarys 类型。
  • @Sarah 转换为char 数组很简单:基本上,您只需将赋值行从binary[counter] = decimal % 2; 更改为binary[counter] = (char)((decimal % 2) + '0'); 此外,sizeof(int)(在malloc)将然后是sizeof(char) - 根据定义,这是1
  • 酷!我能问一下为什么我需要做+'0'吗?
  • 代表数字的字符,'0' 到 '9' 的值是 not 0 到 9 - 尽管根据定义,它们是连续的。在 ASCII(最常用的系统)中,“0”字符的值是 48,而“9”的值是 57。使用'0' 格式可以使您的代码也适用于非 ASCII 系统。
【解决方案2】:

关于:

int binary[size];
    binary[size] = (int *)malloc(size);

这会分配size 字节,这对您不起作用。建议:

int *binary = malloc( size * sizeof( int ) );
if( ! binary )
{
    perror( "malloc failed" );
    exit( EXIT_FAILURE );
}

没有从malloc() 转换返回值,因为在C 中,返回的类型是void*,它可以分配给任何指针并且binary[size] 是一个数组,而不是一个指针。 IE。除了不需要之外,演员阵容也是错误的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 2021-11-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    相关资源
    最近更新 更多