void * to char *? [英] void* to char* ?

查看:70
本文介绍了void * to char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢修理一些东西,所以我正在处理与Borland C ++ 4.52签订的书籍中的程序。我正在尝试用

gcc 3.4.2来修复它们。我不明白是什么导致了以下错误,但是我认为它可能与malloc()有关。任何见解都赞赏。


DAY01E4.C:10:错误:无效转换为`void *''到'char *''


/ *第1天:练习4 * /

#include< stdlib.h>

#include< stdio.h>


#define MAX 100

int main(void)// is void main(void)

{

char * string;

string = malloc(MAX);

printf("输入内容:");

//得到(字符串);

fgets(string,sizeof(string),stdin); //替换gets()

puts(string); / *做类似印刷的事情* /

免费(字符串);


返回0; // for int main()

}

I like fixing things so I''m working on programs from books that came in a
package deal with Borland C++ 4.52. I''m trying to fix them to compile with
gcc 3.4.2. I don''t understand what''s causing the following error but I
think it might have something to do with malloc(). Any insight appreciated.

DAY01E4.C:10: error: invalid conversion from `void*'' to `char*''

/* Day 1: Exercise 4 */
#include <stdlib.h>
#include <stdio.h>

#define MAX 100

int main(void) // was void main(void)
{
char * string;
string = malloc( MAX );
printf( "Enter something: " );
// gets( string );
fgets( string, sizeof(string), stdin ); // to replace gets()
puts( string ); /* do something like printing */
free( string );

return 0; // for int main()
}

推荐答案



bildad写道:

bildad wrote:
我喜欢修复一些东西所以我正在处理与Borland C ++ 4.52一起处理的书籍中的程序。我正在尝试用
gcc 3.4.2来修复它们。我不明白是什么导致了以下错误,但我认为它可能与malloc()有关。任何见解都赞赏。

DAY01E4.C:10:错误:无效转换为`void *''到'char *''

/ *第1天:练习4 * /
#include< stdlib.h>
#include< stdio.h>

#define MAX 100

int main(void) // is void main(void)
{
char * string;
string = malloc(MAX);
printf(" Enter something:");
// gets(string);
fgets(string,sizeof(string),stdin); //替换gets()
puts(string); / *做一些像打印* /
免费(字符串);

返回0; // for int main()
}
I like fixing things so I''m working on programs from books that came in a
package deal with Borland C++ 4.52. I''m trying to fix them to compile with
gcc 3.4.2. I don''t understand what''s causing the following error but I
think it might have something to do with malloc(). Any insight appreciated.

DAY01E4.C:10: error: invalid conversion from `void*'' to `char*''

/* Day 1: Exercise 4 */
#include <stdlib.h>
#include <stdio.h>

#define MAX 100

int main(void) // was void main(void)
{
char * string;
string = malloc( MAX );
printf( "Enter something: " );
// gets( string );
fgets( string, sizeof(string), stdin ); // to replace gets()
puts( string ); /* do something like printing */
free( string );

return 0; // for int main()
}




gcc可能将其编译为C ++程序,在这种情况下C ++

需要来自void *的显式转换(即你必须转换

malloc)。你应该确保扩展名是.c,因为gcc通常是

通过文件名确定源文件的类型,因此使用哪个
编译器。



gcc is probably compiling it as a C++ program in which case C++
requires explicit conversions from void * (i.e. you have to cast the
malloc). You should make sure the extension is .c as gcc usually
determines by the filename the kind of source file and therefore which
compilers to use.


bildad< bi **** @ wi.rr.com>写道:
bildad <bi****@wi.rr.com> wrote:
/ *第1天:练习4 * /
#include< stdlib.h>
#include< stdio.h>
#define MAX 100
int main(void)// is void main(void)


好​​。

{
char * string;
string = malloc(MAX);


您应该检查以确保malloc()成功:


if(!string){

fprintf (Malloc失败); / *或其他* /

退出(EXIT_FAILURE);

}

printf("输入内容:");
// gets(string);
fgets(string,sizeof(string),stdin); //替换gets()


善意但错误。 string是一个指针; sizeof(string)是指针的大小,而不是它所指向的内存大小(如果

确实指向任何指针)。


fgets(string,MAX,stdin);

puts(string); / *做一些像打印* /
free(string);
返回0; // for int main()
}
/* Day 1: Exercise 4 */
#include <stdlib.h>
#include <stdio.h> #define MAX 100 int main(void) // was void main(void)
Good.
{
char * string;
string = malloc( MAX );
You should check to ensure that malloc() succeeded:

if( !string ) {
fprintf( "Malloc failed" ); /* or whatever */
exit( EXIT_FAILURE );
}
printf( "Enter something: " );
// gets( string );
fgets( string, sizeof(string), stdin ); // to replace gets()
Well-intentioned but wrong. string is a pointer; sizeof(string) is
the size of the pointer, not the size of the memory it points to (if
indeed it points to any).

fgets( string, MAX, stdin );
puts( string ); /* do something like printing */
free( string ); return 0; // for int main()
}




-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


bildad< bi **** @ wi.rr.com>写道:
bildad <bi****@wi.rr.com> writes:
我喜欢修理东西所以我正在处理与Borland C ++ 4.52一起处理的书籍中的程序。我正在尝试用
gcc 3.4.2来修复它们。我不明白是什么导致了以下错误,但我认为它可能与malloc()有关。任何见解都赞赏。

DAY01E4.C:10:错误:无效转换为`void *''到'char *''

/ *第1天:练习4 * /
#include< stdlib.h>
#include< stdio.h>

#define MAX 100

int main(void) // is void main(void)
{
char * string;
string = malloc(MAX);
printf(" Enter something:");
// gets(string);
fgets(string,sizeof(string),stdin); //替换gets()
puts(string); / *做一些像打印* /
免费(字符串);

返回0; // for int main()
}
I like fixing things so I''m working on programs from books that came in a
package deal with Borland C++ 4.52. I''m trying to fix them to compile with
gcc 3.4.2. I don''t understand what''s causing the following error but I
think it might have something to do with malloc(). Any insight appreciated.

DAY01E4.C:10: error: invalid conversion from `void*'' to `char*''

/* Day 1: Exercise 4 */
#include <stdlib.h>
#include <stdio.h>

#define MAX 100

int main(void) // was void main(void)
{
char * string;
string = malloc( MAX );
printf( "Enter something: " );
// gets( string );
fgets( string, sizeof(string), stdin ); // to replace gets()
puts( string ); /* do something like printing */
free( string );

return 0; // for int main()
}




显然,malloc调用在第10行,但这并不明显

不计算行数。添加/ *行10 * /评论会有帮助。

有用。


上面的程序是有效的C,但是C ++是无效的。 gcc假定带有.C的

文件后缀是C ++。使用.c重命名文件。后缀。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Apparently the malloc call is on line 10, but that''s not obvious
without counting lines. Adding a "/* line 10 */" comment would have
been helpful.

The program above is valid C, but invalid C++. gcc assumes that a
file with a ".C" suffix is C++. Rename the file with a ".c" suffix.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于void * to char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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