C语言 C: 无法使用 void* 类型的右值初始化变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24227723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:10:40  来源:igfitidea点击:

C: Cannot initialize variable with an rvalue of type void*

cpointersvariable-initialization

提问by user3662185

I have the following code:

我有以下代码:

int *numberArray = calloc(n, sizeof(int));

And I am unable to understand why I receive the following error.

我无法理解为什么会收到以下错误。

Cannot initialize a variable of type 'int *' with an rvalue of type 'void *'`.

Thank you.

谢谢你。

回答by R Sahu

The compiler's error message is very clear.

编译器的错误信息非常清楚。

The return value of callocis void*. You are assigning it to a variable of type int*.

的返回值callocvoid*。您将其分配给类型为 的变量int*

That is ok with a C program, but not with a C++ program.

这对 C 程序没问题,但对 C++ 程序则不行。

You can change that line to

您可以将该行更改为

int* numberArray = (int*)calloc(n, sizeof(int));

But, a better alternative will be to use the newoperator to allocate memory. After all, you are using C++.

但是,更好的选择是使用new运算符来分配内存。毕竟,您正在使用 C++。

int* numberArray = new int[n];

回答by user3662185

void* calloc (size_t num, size_t size);

Allocate and zero-initialize array. Allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all its bits to zero.The effective result is the allocation of a zero-initialized memory block of (num*size) bytes.

On success, a pointer to the memory block allocated by the function. The type of this pointer is always void*, which can be cast to the desired type of data pointer in order to be dereferenceable. If the function failed to allocate the requested block of memory, a null pointer is returned.

分配和零初始化数组。为 num 个元素的数组分配一块内存,每个元素的 size 字节长,并将其所有位初始化为零。有效的结果是分配一个 (num*size) 字节的零初始化内存块。

成功时,指向函数分配的内存块的指针。此指针的类型始终为 void*,可以将其强制转换为所需的数据指针类型,以便取消引用。如果函数未能分配请求的内存块,则返回空指针。

To summarize, since callocreturns a void*(generic pointer) on success of memory allocation, you will have to type-cast it like this in C++:

总而言之,由于在内存分配成功时calloc返回一个void*(通用指针),您必须在 C++ 中像这样对其进行类型转换:

int *numberArray = (int*)calloc(n, sizeof(int));

If it was C, you can still skip this cast.

如果是 C,你仍然可以跳过这个演员表。

Or, use newas:

或者,new用作:

int *numberArray = new int [n];

回答by Hussain

Syntax for calloc is:

calloc 的语法是:

void* calloc (size_t num, size_t size);

void* calloc (size_t num, size_t size);

calloc returns void pointer. In your code , you are trying to assign void pointer to integer pointer. So you are getting Cannot initialize a variable of type 'int *' with an rvalue of type 'void *'. So typecast the void pointer before assign it like this

calloc 返回空指针。在您的代码中,您试图将 void 指针分配给整数指针。因此,您将无法使用类型为“void *”的右值初始化“int *”类型的变量。所以在像这样分配之前对void指针进行类型转换

*numberArray = (int *) calloc(n, sizeof(int));

*numberArray = (int *) calloc(n, sizeof(int));

回答by AK_

You're using C memory re-allocation style in a C++ code. What you want to use is newin C++

您在 C++ 代码中使用 C 内存重新分配样式。你想用的是newC++

So your code will become:

所以你的代码将变成:

int n = 10; //n = size of your array
int *narray = new int[n];
for (int i = 0; i < n; i++)
    cout << narray[i];

Alternatively you can switch back to C and use calloc with casting.

或者,您可以切换回 C 并使用 calloc 进行转换。

int* numberArray = (int*)calloc(n, sizeof(int));