#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/