[英]warning: return from incompatible pointer type [-Wincompatible-pointer-types]|
I am trying to return an array from the function pointer, the code work but shows a warning in C that "incompatible pointer type".我正在尝试从 function 指针返回一个数组,代码工作但在 C 中显示“指针类型不兼容”的警告。 I want to return an array and it is already dynamic allocated.
我想返回一个数组,它已经是动态分配的。 Can somebody tell me the problem and the solution to it
有人可以告诉我问题和解决方案吗
#include <stdlib.h>
#include<stdio.h>
unsigned short *reverse_seq(unsigned short num)
{
if(num==0) return NULL;
int size=num+1;
int* numbers=(int *) malloc(size*sizeof(int));
for(int i=0;i<num;i++){
numbers[i]=num-i;
}
for(int i=0;i<num;i++){
printf("%d ",numbers[i]);
}
return numbers;
}
int main(void)
{
int num=5;
reverse_seq(num);
return 0;
}
Can somebody give me the solution to this warning?有人可以给我这个警告的解决方案吗?
unsigned short *
but you allocate space for int
s and try to return an int*
.unsigned short *
但您为int
分配空间并尝试返回int*
。 I assume you want to store unsigned short
s in the allocated memory.unsigned short
存储在分配的 memory 中。free
the allocated memory.free
分配的 memory。#include <stdio.h>
#include <stdlib.h>
unsigned short *reverse_seq(unsigned short num) {
if (num == 0) return NULL;
// corrected allocation (there's no need for num + 1 elements either):
unsigned short *numbers = malloc(num * sizeof *numbers);
if(numbers) { // check that allocation worked
for (int i = 0; i < num; i++) {
numbers[i] = num - i;
}
}
// printing moved to `main` to make use of the data there
return numbers;
}
int main(void) {
unsigned short num = 5; // same type as `reverse_seq` wants
unsigned short *numbers = reverse_seq(num);
if(numbers) { // again, check that allocation worked
for (int i = 0; i < num; i++) {
printf("%d ", numbers[i]);
}
free(numbers); // free the memory
}
}
Try尝试
#include <stdio.h>
#include <stdlib.h>
unsigned short *reverse_seq(unsigned short num) {
if (num == 0) return NULL;
int size = num + 1;
unsigned short *numbers = malloc((size_t) size * sizeof(unsigned short)); // THIS IS THE IMPORTANT CHANGE
for (int i = 0; i < num; i++) {
numbers[i] = (unsigned short) (num - i);
}
for (int i = 0; i < num; i++) {
printf("%d ", numbers[i]);
}
return numbers;
}
int main(void) {
int num = 5;
unsigned short *res = reverse_seq((unsigned short) num);
free(res);
return 0;
}
This will fix your major problem with the original warning.这将解决原始警告的主要问题。
The important thing is to allocate memory of the correct type of what you are returning.重要的是分配您返回的正确类型的 memory。 The problem is that you are returning a
int*
when a pointer to unsigned short*
is requested.问题是当请求指向
unsigned short*
的指针时,您正在返回一个int*
。 Not doing this has consequences for accessing the memory later on since the memory layout of the access from the returned pointer does not match how you allocated your memory sequence.不这样做会对稍后访问 memory 产生影响,因为从返回的指针访问的 memory 布局与您分配 memory 序列的方式不匹配。
If you want to return an array of unsigned short
you should allocate an array of unsigned short
but not of int
s:如果你想返回一个
unsigned short
数组,你应该分配一个unsigned short
数组,而不是int
s:
unsigned short* reverse_seq(unsigned short num)
{
if (num == 0)
return NULL;
unsigned short* numbers = malloc(num * sizeof(unsigned short));
if (numbers) {
for (unsigned short i = 0; i < num; i++) {
numbers[i] = num - i;
}
}
return numbers;
}
You are allocating an array of integers, but you are returning an array of shorts (which normally are half the size of an integer).您正在分配一个整数数组,但您正在返回一个短裤数组(通常是整数大小的一半)。 Just return a pointer to
int
, as in:只需返回一个指向
int
的指针,如下所示:
int *reverse_seq(unsigned short num) {
/* ... */
or allocate an array of shorts, as in:或分配一个短裤数组,如:
unsigned short *numbers = malloc(num * sizeof *numbers); /* and please, ***never*** cast the result of malloc() */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.