简体   繁体   English

警告:从不兼容的指针类型返回 [-Wincompatible-pointer-types]|

[英]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?有人可以给我这个警告的解决方案吗?

  • Your function is declared to return unsigned short * but you allocate space for int s and try to return an int* .您的 function 被声明为返回unsigned short *但您为int分配空间并尝试返回int* I assume you want to store unsigned short s in the allocated memory.我假设您想将unsigned short存储在分配的 memory 中。
  • When you return a pointer to dynamically allocated memory, you should always assign that pointer to a variable so that you can free the allocated memory.当您返回指向动态分配的 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.

相关问题 来自不兼容指针类型的赋值[Wincompatible-pointer-types] - Assignment from incompatible pointer type[Wincompatible-pointer-types] 来自不兼容的指针类型[-Wincompatible-pointer-types]的赋值 - assignment from incompatible pointer type [-Wincompatible-pointer-types] 在 c 中将大端转换为小端。 警告:从不兼容的指针类型返回 [-Wincompatible-pointer-types] 返回 src; - converting Big endian to little endian in c. warning:return from incompatible pointer type [-Wincompatible-pointer-types] return src; RecursiveFree函数-警告:从不兼容的指针类型[-Wincompatible-pointer-types]进行初始化 - RecursiveFree Function - Warning: initialization from incompatible pointer type [-Wincompatible-pointer-types] C中的函数指针:警告:来自不兼容指针类型[-Wincompatible-pointer-types]的赋值 - Function pointers in C: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] 警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“接受”的参数 2 - warning: passing argument 2 of ‘accept’ from incompatible pointer type [-Wincompatible-pointer-types] 从不兼容的指针类型 [-Wincompatible-pointer-types] 获取传递“pthread_create”参数 3 的警告 - Getting warning of passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types] 警告:从不兼容的指针类型 [-Wincompatible-pointer-types]| 传递 'transform_labels' 的参数 2 | - Warning: passing argument 2 of 'transform_labels' from incompatible pointer type [-Wincompatible-pointer-types]| 警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递 'display' 的参数 1 - warning: passing argument 1 of 'display' from incompatible pointer type [-Wincompatible-pointer-types] 警告:C 中不兼容的指针类型 [-Wincompatible-pointer-types] - warning: incompatible pointer types [-Wincompatible-pointer-types] in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM