c - 非可移植指针转换警告

标签 c pointers

#include<stdio.h>
#include<conio.h>
int main()
{
    char *p=malloc(100);
    clrscr();
    printf("enter your name\n");
    scanf("%s",p);
    printf("%s",p);
    getch();
    return 0;
}

编译这个c程序时出现“不可移植指针转换”警告。警告的原因是什么?

最佳答案

The warning is solved..why it is occuring? please explain..?

您缺少 malloc 的原型(prototype),因此编译器假定返回类型为 int

char *p=malloc(100);

int 分配给 char* 会给出警告。显然它们在您的编译器中的大小相同,但它们在所有地方的大小都不相同。

当您添加原型(prototype)时(在您的情况下包括 stdlib),编译器被告知 malloc 的返回类型是 void*void* 是一种指针类型,无需强制转换即可分配给任何其他指针类型。

关于c - 非可移植指针转换警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23236966/

相关文章:

c - 带指针的 toupper 和 tolower

c - 为什么这个 Scanf 会导致无限循环?

c - 调用open时如何调用sys_open而不是sys_openat

c++ - 乘以 1.0 和 int 到浮点转换的精度

c - 数据设计: better to nest structures or pointers to structures?

c - 指针程序

java - C转java文件读取和打印

c - 如何让 GCC 在没有内置函数的情况下为大端存储生成 bswap 指令?

const char* 指针运算警告

C 编译错误指针