【问题标题】:C error: invalid operands of types 'int*' and 'unsigned int' to binary 'operator*'|C 错误:“int*”和“unsigned int”类型的无效操作数到二进制“operator*”|
【发布时间】:2013-05-05 16:30:16
【问题描述】:

所以,我在 C 函数中得到了这个错误。

变量:

int* first_array = (int*) malloc(0);
int first_array_length;

int* second_array = (int*) malloc(0);
int second_array_length;

// Setting up first array
set_up_array(first_array, &first_array_length);

这是一个函数:

void set_up_array(int *arr, int *num)
{
    char lenght_msg[] = "Iveskite masyvo ilgi";
    char value_msg[] = "Iveskite masyvo elementa";

    *num = num_scan(0, MAX_SIZE, lenght_msg);
    arr = (int*) realloc(arr, num * sizeof(int)); // <-  error here 
    for (int i = 0; i < (*num); i++)
    {
        arr[i] =  num_scan(INT_MIN, INT_MAX, value_msg);
    }
}

请帮忙!

错误:

“int*”和“unsigned int”类型的无效操作数到二进制“operator*”|

【问题讨论】:

  • int *arr --> int **arr 因为它不反映变化。

标签: c pointers realloc


【解决方案1】:

使用*num 代替num

realloc(arr, num * sizeof(int));

num是指向int的指针,int指针的值是*num

而且您不应该强制转换 realloc 返回值。

http://c-faq.com/malloc/mallocnocast.html

【讨论】:

    猜你喜欢
    • 2022-07-22
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多