使用函数初始化C中的数组变量

使用函数初始化C中的数组变量,c,C,我是一个完全的初学者 我有一个将int转换为数组的函数 #include <math.h> #include <stdio.h> int *convertIntArray(int number) { int n = log10(number) + 1; int i; int *numberArray = calloc(n, sizeof(int)); for (i = 0; i < n; ++i, number /= 10) {

我是一个完全的初学者

我有一个将
int
转换为
数组的函数

#include <math.h> 
#include <stdio.h>

int *convertIntArray(int number) {
    int n = log10(number) + 1;
    int i;
    int *numberArray = calloc(n, sizeof(int));
    for (i = 0; i < n; ++i, number /= 10) {
        numberArray[i] = number % 10;
    }
    return numberArray;
}

但很明显,这是行不通的。正确的方法是什么?

数组由
converIntArray
函数分配,并返回指向其第一个元素的指针。如果使用适当的类型定义
array
,则可以将指向
array
的指针存储在
main
中:

int main(int argc, char *argv[]) {
    int *sample = convertIntArray(150);
    // rest of main function

但是请注意,您无法从指针和值判断
array
指向的数组的大小。您应该返回始终分配最大大小,即10
int
,或者以其他方式返回大小,例如作为数组的第一个元素,然后使用额外的元素进行分配。

在代码中首先要注意的是,您没有包含函数
log10()的原型
我想应该在
中。或者您已经完成了,但没有在代码片段中显示它。这是StackOverflow中的糟糕帖子,因为这让我们陷入了判断您是否犯了错误的两难境地(不包括错误,这将给您未定义的行为),因此,请编辑您的问题,包括错误

如果我必须编写您的函数,我不应该使用
log10()
,因为它是一个浮点函数,如果计算中出现舍入错误,您可以选择错误的位数进行分配。无论如何,我会尝试使用尽可能少的支持函数,将您的接口更改为:

unsigned *number2digits(
    unsigned *array_to_work_in,  /* the array is provided to the function */ 
    size_t array_size,           /* the size of the array, in number of elements */
    unsigned source_number,      /* the number to be converted */
    unsigned base);              /* the base to use for conversion */
这种方法不会强迫您只为
log10()
函数包含数学库,也不会使用动态分配器例程
malloc()
calloc()
,如果函数不调用更多的外部/库函数,编译器会对其进行更好的优化如果要在已分配的数组上使用该函数,则不必重写它。此外,编号基数作为参数传递是一件好事,因此您不必只使用以10为基数的数字

另一个错误是,您正在返回一个
int*
(指向
int
)的指针,并将该值赋给
int
类型的变量(这是一个应该给您带来编译器错误的错误)

有效(完整且可验证)示例代码应为:

#include <stdio.h>
#include <stdlib.h>

unsigned *number2digits(
    unsigned *array_to_work_in,  /* the array is provided to the function */ 
    size_t array_size,           /* the size of the array, in number of elements */
    unsigned source_number,      /* the number to be converted */
    unsigned base)               /* the base to use for conversion */
{
    size_t i;
    for (i = 0; i < array_size; i++) {
        array_to_work_in[i] = source_number % base;
        source_number /= base;
    }
    /* if, at this point, source_number != 0, you made an error
     * on estimating array size, as the number doesn't fit in
     * array_size digits. In case of error, we return NULL to
     * indicate the error. */
    return source_number == 0 ? array_to_work_in : NULL;
}

#define NUMBER 150000
#define N 10
#define BASE 38

int main()
{
    unsigned my_array[N]; /* 10 digits for a base 38 is enough for a 32 bit integer */
    unsigned *r = number2digits(my_array, N, NUMBER, BASE);
    if (!r) {
        fprintf(stderr, "Error, number %u does not fit in %u base %u digits\n",
            NUMBER, N, BASE);
        exit(EXIT_FAILURE);
    }
    printf("%u =/base %u/=>", NUMBER, BASE);
    int i; /* we use int here as size_t is unsigned and we could
            * not detect the loop exit condition */
    for (i = N-1; i >= 0; i--) {
        printf(" %u", r[i]);
    }
    printf("\n");
    exit(EXIT_SUCCESS);
}

此代码中没有数组变量。指针不是数组,数组也不是指针。@Eugene:是的,区别很重要,但
numberArray
是指向数组第一个元素的指针。伪代码将
sample
定义为
int
对象。您打算
sample
作为数组吗?例如,
int-sample[]=convertIntArray(150);
(这也行不通)?@EugeneSh.您错了,函数中有一个数组变量,只需查看从
calloc(3)
返回的值是指向整数数组的指针。尽管函数返回指针,但该指针的解引用是请求的数组。
#include <stdio.h>
#include <stdlib.h>

unsigned *number2digits(
    unsigned *array_to_work_in,  /* the array is provided to the function */ 
    size_t array_size,           /* the size of the array, in number of elements */
    unsigned source_number,      /* the number to be converted */
    unsigned base)               /* the base to use for conversion */
{
    size_t i;
    for (i = 0; i < array_size; i++) {
        array_to_work_in[i] = source_number % base;
        source_number /= base;
    }
    /* if, at this point, source_number != 0, you made an error
     * on estimating array size, as the number doesn't fit in
     * array_size digits. In case of error, we return NULL to
     * indicate the error. */
    return source_number == 0 ? array_to_work_in : NULL;
}

#define NUMBER 150000
#define N 10
#define BASE 38

int main()
{
    unsigned my_array[N]; /* 10 digits for a base 38 is enough for a 32 bit integer */
    unsigned *r = number2digits(my_array, N, NUMBER, BASE);
    if (!r) {
        fprintf(stderr, "Error, number %u does not fit in %u base %u digits\n",
            NUMBER, N, BASE);
        exit(EXIT_FAILURE);
    }
    printf("%u =/base %u/=>", NUMBER, BASE);
    int i; /* we use int here as size_t is unsigned and we could
            * not detect the loop exit condition */
    for (i = N-1; i >= 0; i--) {
        printf(" %u", r[i]);
    }
    printf("\n");
    exit(EXIT_SUCCESS);
}
$ pru
150000 =/base 38/=> 0 0 0 0 0 0 2 27 33 14
$ _