警告:return 来自不兼容的指针类型 [-Wincompatible-pointer-types]|
warning: return from incompatible pointer type [-Wincompatible-pointer-types]|
我正在尝试从函数指针 return 一个数组,代码可以工作,但在 C 中显示“不兼容的指针类型”的警告。我想要 return 一个数组,它已经是动态分配的。谁能告诉我问题和解决方法
#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;
}
有人可以给我这个警告的解决方案吗?
尝试
#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;
}
这将解决原始警告的主要问题。
重要的是为您要返回的内容分配正确类型的内存。问题是当请求指向 unsigned short*
的指针时,您返回 int*
。不这样做
对以后访问内存有影响,因为从返回的指针访问的内存布局与您分配内存序列的方式不匹配。
如果你想要 return 一个 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;
}
- 您的函数声明为 return
unsigned short *
,但您为 int
分配了 space 并尝试 return 和 int*
。我假设您想将 unsigned short
s 存储在分配的内存中。
- 当您 return 一个指向动态分配内存的指针时,您应该始终将该指针分配给一个变量,以便您可以
free
分配的内存。
#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
}
}
您正在分配一个整数数组,但您正在 return 分配一个短裤数组(通常是整数大小的一半)。只是 return 指向 int
的指针,如:
int *reverse_seq(unsigned short num) {
/* ... */
或者分配一个短裤数组,如:
unsigned short *numbers = malloc(num * sizeof *numbers); /* and please, ***never*** cast the result of malloc() */
我正在尝试从函数指针 return 一个数组,代码可以工作,但在 C 中显示“不兼容的指针类型”的警告。我想要 return 一个数组,它已经是动态分配的。谁能告诉我问题和解决方法
#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;
}
有人可以给我这个警告的解决方案吗?
尝试
#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;
}
这将解决原始警告的主要问题。
重要的是为您要返回的内容分配正确类型的内存。问题是当请求指向 unsigned short*
的指针时,您返回 int*
。不这样做
对以后访问内存有影响,因为从返回的指针访问的内存布局与您分配内存序列的方式不匹配。
如果你想要 return 一个 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;
}
- 您的函数声明为 return
unsigned short *
,但您为int
分配了 space 并尝试 return 和int*
。我假设您想将unsigned short
s 存储在分配的内存中。 - 当您 return 一个指向动态分配内存的指针时,您应该始终将该指针分配给一个变量,以便您可以
free
分配的内存。
#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
}
}
您正在分配一个整数数组,但您正在 return 分配一个短裤数组(通常是整数大小的一半)。只是 return 指向 int
的指针,如:
int *reverse_seq(unsigned short num) {
/* ... */
或者分配一个短裤数组,如:
unsigned short *numbers = malloc(num * sizeof *numbers); /* and please, ***never*** cast the result of malloc() */