问题描述
/ *开始端口到c ++ * /
#ifdef __cplusplus
externC" ; {
#endif
int * get_an_int(void);
int * pass_pointer_triv(int *);
#ifdef __cplusplus
}
#endif
/ *结束端口到c ++ * /
#include< stdio.h>
#include< stdlib.h>
int main(void)
{
int * rt,* qt;
qt = get_an_int();
rt = pass_pointer_triv(qt );
printf(t is%d \ n,* rt);
printf(" tja \ n");
返回0;
}
int * get_an_int(无效)
{
int t;
int * pt;
pt = malloc(sizeof * pt);
t = 41;
pt [0] = t;
返回pt;
}
pass_pointer_triv(int * a)
{
int t;
t = * a;
printf(" t is%d \ n", t);
返回a;
}
/ *结束来源* /
我相信这个来源是一个愚蠢的错误,远离kosher c ++。
我的编译器告诉我,微不足道的指针传递在
间接的水平上有所不同,但在我看来,这一切都是看起来一样。我相信我的
编译器是正确的。感谢任何提示。 furunculus
/* begin port to c++ */
# ifdef __cplusplus
extern "C" {
# endif
int * get_an_int(void);
int * pass_pointer_triv(int *);
# ifdef __cplusplus
}
# endif
/* end port to c++ */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *rt, *qt;
qt = get_an_int();
rt = pass_pointer_triv(qt);
printf ("t is %d\n", *rt);
printf ("tja\n");
return 0;
}
int * get_an_int(void)
{
int t;
int * pt;
pt = malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
pass_pointer_triv(int *a)
{
int t;
t = *a;
printf("t is %d\n", t);
return a;
}
/* end source */
I believe that this source is one silly error away from being kosher c++.
My compiler tells me that the trivial pointer pass varies in level of
indirection, but to my eye, it all looks the same. I believe that my
compiler is correct. Grateful for any hints. furunculus
推荐答案
我的编译器抱怨这个。
错误C2440:''='':无法从''void *''转换为''int *''
无论是强制转换为int *还是使用new(首选)。
My compiler complains about this.
error C2440: ''='' : cannot convert from ''void *'' to ''int *''
Either typecast to int * or use new (preferred) instead.
请解释。
Please explain.
您忘记了返回类型。将此更改为
int * pass_pointer_triv(int * a)
并为它编译,除了malloc错误。
You forgot the return type. Change this to
int* pass_pointer_triv(int *a)
and it compiles for me, except for the malloc error.
我认为那会'让她去谢谢你假装我的问题
今晚是你的问题。 ciao,f
I think that''s gonna get her going. Thanks for pretending like my problems
were your problems tonight. ciao, f
请解释。
Please explain.
malloc返回void *(void指针)。如果你在c ++中使用它,你需要
使用reinterpret_cast或c-style
强制转换为正确的类型。这些工作中的任何一个,reinterpert_cast首选。
pt = reinterpret_cast< int *>(malloc(sizeof * pt));
pt =(int *)malloc(sizeof * pt);
但是首选使用new。
int * pt = new int;
那么只需使用delete来摆脱内存而不是免费(你使用带有malloc的
,但你的代码中缺少你的内存泄漏)
malloc returns a void* (void pointer). If you use it in c++ you need to
typecast it to the proper type either with reinterpret_cast or a c-style
cast. Either of these work, the reinterpert_cast prefered.
pt = reinterpret_cast<int*>( malloc( sizeof *pt ) );
pt = (int*) malloc( sizeof *pt );
but prefered is using new.
int *pt = new int;
Then just use delete to get rid of the memory instead of free (which you use
with malloc, but you are missing in your code so you have a memory leak)
您忘记了返回类型。将此更改为
int * pass_pointer_triv(int * a)
并为我编译,除了malloc错误。
You forgot the return type. Change this to
int* pass_pointer_triv(int *a)
and it compiles for me, except for the malloc error.
我认为这会让她继续前进。感谢像我这样的问题假装你今晚的问题。 ciao,f
I think that''s gonna get her going. Thanks for pretending like my
problems were your problems tonight. ciao, f
这篇关于窃听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!