要求malloc从malloc返回吗? [英] is typecasting return from malloc required?

查看:84
本文介绍了要求malloc从malloc返回吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




为什么我会收到以下错误?我是否需要明确地将malloc返回的

指针强制转换为char *?


谢谢,

ceo


[root @ lin1 tmp] #cat malloc.cpp

#include< stdio.h>

#include< malloc.h> ;

#include< string.h>


int main(){


char * s = NULL;

s = malloc(sizeof(char)* 10);

strcpy(s,blah blah);

printf( \ n%s \ n,s);

返回0;

}


[root @ lin1 tmp] #gcc malloc.cpp

malloc.cpp:在函数`int main()'':

malloc.cpp:8:无法转换`void *'分配给'char *''

[root @ lin1 tmp] #gcc -v

从/ usr / lib / gcc-lib / i386-redhat读取规范-linux / 2.96 / specs

gcc版本2.96 20000731(Red Hat Linux 7.2 2.96-108.1)

[root @ lin1 tmp]#

解决方案

ceo写道:

为什么我会收到以下错误?我是否需要明确地将malloc返回的指针强制转换为char *?

谢谢,


[root @ lin1 tmp] #cat malloc.cpp
#include< stdio.h>
#include< malloc.h>
#include< string.h>

int main( ){

char * s = NULL;
s = malloc(sizeof(char)* 10);
strcpy(s,blah blah);
printf(" \ n%s \ n",s);
返回0;
}

[root @ lin1 tmp] #gcc malloc.cpp < broc> malloc.cpp:在函数`int main()''中:
malloc.cpp:8:在赋值时无法将`void *''转换为`char *''
[root @ lin1 tmp] #gcc -v
从/usr/lib/gcc-lib/i386-redhat-linux/2.96/specs阅读规范
gcc版本2.96 20000731(Red Hat Linux 7.2 2.96-108.1)
[root @ lin1 tmp]#




提示:哪个头文件是malloc声明的?


>提示:哪个头文件是malloc声明的?


你好akarl,
如果我包含stdlib.h,那么
i会得到同样的错误这就是你的意思。


谢谢,

ceo


[root @ lin1 tmp] #cat malloc .cpp

#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int main(){


char * s = NULL;

s = malloc(sizeof(char) * 10);

strcpy(s,blah blah);

printf(" \ n%s \ n",s);

返回0;

}


[root @ lin1 tmp] #gcc malloc.cpp

malloc .cpp:在函数`int main()''中:

malloc.cpp:8:在赋值时无法将`void *''转换为`char *''

[root @ lin1 tmp] #gcc -v

从/usr/lib/gcc-lib/i386-redhat-linux/2.96/specs阅读规格

gcc版本2.96 20000731(Red Hat Linux 7.2 2.96-108.1)

[root @ lin1 tmp]#


因为它是作为C ++代码编译的。


如果更改文件名 ; malloc.cpp" tomalloc.c,

添加类型转换操作符,或者

使用new []操作符,可以避免该错误。


但是如果用gcc编译C ++代码(如果你尝试第二个或第三个)或者

" cc"命令

您将收到链接错误。为避免此错误,您应该使用

" g ++"进行编译。或者c ++


谢谢,

Norihiro Kamae在日本


ceo写道:



为什么我会收到以下错误?我是否需要明确地将malloc返回的指针强制转换为char *?

谢谢,


[root @ lin1 tmp] #cat malloc.cpp
#include< stdio.h>
#include< malloc.h>
#include< string.h>

int main( ){

char * s = NULL;
s = malloc(sizeof(char)* 10);
strcpy(s,blah blah);
printf(" \ n%s \ n",s);
返回0;
}

[root @ lin1 tmp] #gcc malloc.cpp < broc> malloc.cpp:在函数`int main()''中:
malloc.cpp:8:在赋值时无法将`void *''转换为`char *''
[root @ lin1 tmp] #gcc -v
从/usr/lib/gcc-lib/i386-redhat-linux/2.96/specs阅读规范
gcc版本2.96 20000731(Red Hat Linux 7.2 2.96-108.1)
[root @ lin1 tmp]#



hi,

why do i get the following error? do i need to explicitly typecast the
pointer returned by malloc to char *?

thanks,
ceo

[root@lin1 tmp]# cat malloc.cpp
#include <stdio.h>
#include <malloc.h>
#include <string.h>

int main() {

char *s = NULL;
s = malloc(sizeof( char) * 10);
strcpy(s, "blah blah");
printf("\n%s\n", s);
return 0;
}

[root@lin1 tmp]# gcc malloc.cpp
malloc.cpp: In function `int main ()'':
malloc.cpp:8: cannot convert `void *'' to `char *'' in assignment
[root@lin1 tmp]# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.2 2.96-108.1)
[root@lin1 tmp]#

解决方案

ceo wrote:

hi,

why do i get the following error? do i need to explicitly typecast the
pointer returned by malloc to char *?

thanks,
ceo

[root@lin1 tmp]# cat malloc.cpp
#include <stdio.h>
#include <malloc.h>
#include <string.h>

int main() {

char *s = NULL;
s = malloc(sizeof( char) * 10);
strcpy(s, "blah blah");
printf("\n%s\n", s);
return 0;
}

[root@lin1 tmp]# gcc malloc.cpp
malloc.cpp: In function `int main ()'':
malloc.cpp:8: cannot convert `void *'' to `char *'' in assignment
[root@lin1 tmp]# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.2 2.96-108.1)
[root@lin1 tmp]#



Hint: In which header file is malloc declared?


> Hint: In which header file is malloc declared?

hello akarl,

i get the same error if i include stdlib.h if that''s what you meant.

thanks,
ceo

[root@lin1 tmp]# cat malloc.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

char *s = NULL;
s = malloc(sizeof( char) * 10);
strcpy(s, "blah blah");
printf("\n%s\n", s);
return 0;
}

[root@lin1 tmp]# gcc malloc.cpp
malloc.cpp: In function `int main ()'':
malloc.cpp:8: cannot convert `void *'' to `char *'' in assignment
[root@lin1 tmp]# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.2 2.96-108.1)
[root@lin1 tmp]#


Because it was compiled as an C++ code.

If you change the file name "malloc.cpp" to "malloc.c",
add typecast operator, or
use new[] operator, you can avoid that error.

But if you compile C++ code (if you try 2nd or 3rd one) with "gcc" or
"cc" command
you will get a link error. To avoid this error you should compile with
"g++" or "c++"

thanks,
Norihiro Kamae in Japan

ceo wrote:

hi,

why do i get the following error? do i need to explicitly typecast the
pointer returned by malloc to char *?

thanks,
ceo

[root@lin1 tmp]# cat malloc.cpp
#include <stdio.h>
#include <malloc.h>
#include <string.h>

int main() {

char *s = NULL;
s = malloc(sizeof( char) * 10);
strcpy(s, "blah blah");
printf("\n%s\n", s);
return 0;
}

[root@lin1 tmp]# gcc malloc.cpp
malloc.cpp: In function `int main ()'':
malloc.cpp:8: cannot convert `void *'' to `char *'' in assignment
[root@lin1 tmp]# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.2 2.96-108.1)
[root@lin1 tmp]#



这篇关于要求malloc从malloc返回吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆