无法返回int数组 [英] Cannot return int array

查看:99
本文介绍了无法返回int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想使用studio.h库通过使用数组存储余数来将十进制数转换为二进制数,但结果不正确,也许我在内存分配方面存在问题或返回值错误,请帮我检查一下。
非常感谢!

I want to use only studio.h library to convert from decimal number to binary number by using an array to store remainder but the result is not correct, maybe i have problem with memory allocation or return value is wrong, please help me to check it. Thank you so much!

#include <stdio.h>
int  n = 0;
int* DecimalToBinary(int number){
    int a[10];      
    while(number!=0){
        a[n++] = number%2;
        number/=2;
    }
    return a;
}

void main(){

    int *d1 = DecimalToBinary(5);
    int *d2 = DecimalToBinary(10);

    for(int i = n-1 ;i>=0;i--)
        printf(" %d",d1[i]);

    printf("\n");

    for(int i = n-1 ;i>=0;i--)
        printf(" %d",d2[i]);

}


推荐答案

返回指向本地分配的数组的指针。它分配在堆栈上,并在函数返回时消失,使指针指向垃圾。

You are returning a pointer to a locally allocated array. It is allocated on the stack, and goes away when the function returns, leaving your pointer pointing to garbage.

您有一些选择。您可以传递一个数组来填充:

You have a few options. You could pass an array in to fill:

void DecimalToBinary(int result[10],int number){
    while(number!=0){
        result[n++] = number%2;
        number/=2;
    }
    return result;
}

// usage example:
int b[10];
DecimalToBinary(b, 42);

或者您可以在堆上分配一个数组:

Or you could allocate an array on the heap:

int* DecimalToBinary(int number){
    int *a = (int *)malloc(sizeof(int) * 10);
    while(number!=0){
        a[n++] = number%2;
        number/=2;
    }
    return a;
}

// usage example
int *b = DecimalToBinary(42);
free(b); // when finished with it

或者您也可以将数组包装在struct中:

Or you could wrap the array in a struct:

typedef struct {
    int b[10];
} result;

result DecimalToBinary(int number){
    result r;
    while(number!=0){
        r.b[n++] = number%2;
        number/=2;
    }
    return r;
}

// usage example
result r = DecimalToBinary(42);

如果执行malloc()选项,请不要忘记在返回时释放()返回的数据做完了,否则它会徘徊。这称为内存泄漏。在更复杂的程序中,它可能会导致严重的问题。

If you do the malloc() option, do not forget to free() the returned data when you're done with it, otherwise it will hang around. This is called a memory leak. In more complex programs, it can lead to serious issues.

注意:顺便说一句,如果您的数字大于1023(10个二进制数字),则会超出要求数组。您可能还希望在存储10位数字或传递数组的大小或先计算所需的大小并分配足够的空间后明确停止。另外,如果您的数字为负,您将得到一些奇怪的结果,您可能希望使用 number& 1 而不是 number%2

Note: By the way, if your number is larger than 1023 (10 binary digits), you'll overrun the array. You may also wish to explicitly stop once you've stored 10 digits, or pass the size of the array in, or compute the required size first and allocate that much space. Also, you will get some odd results if your number is negative, you might want to use number&1 instead of number%2.

注2:如其他地方所述,您应将 n 设为本地,或至少每次调用该函数时,将其重新设置为0,否则它将累积,最终您将越过数组的末尾。

Note 2: As noted elsewhere, you should make n local, or at the very least reinitalize it to 0 each time the function is called, otherwise it will just accumulate and eventually you'll go past the end of the array.

这篇关于无法返回int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆