确定变量类型的方法? [英] Way to determine type of variable?
问题描述
我想编写一个通用程序,它将采用字符串或数字
变量。我想不出一种方法可以让这个更清楚,除了显示
我想要的东西。
int main(无效)
{
int i = 7;
char * s =" / etc / filesystems" ;;
generic( i);
generic(s);
generic(" Hello");
generic(2048);
退出(0);
}
应该返回:
/ etc / filesystems
你好
2048
所以...
无效通用(???????)
{
???
}
我看过sprintf,varargs等。
有人能指出我正确的方向吗?
谢谢,
Walter
I would like to write a generic procedure that will take string or numeric
variables. I can not think of a way to make this more clear except to show
what I want.
int main(void)
{
int i=7;
char *s="/etc/filesystems";
generic(i);
generic(s);
generic("Hello");
generic(2048);
exit(0);
}
should return:
7
/etc/filesystems
Hello
2048
so...
void generic( ???????)
{
???
}
I have looked at sprintf, varargs etc.
Could someone point me in the right direction?
Thanks,
Walter
推荐答案
Walter L. Preuninger II< wa ***** @ texramp.net>写道:
Walter L. Preuninger II <wa*****@texramp.net> wrote:
我想写一个通用程序,它将采用字符串或数字
变量。除了展示我想要的东西之外,我想不出一种方法可以让它更清晰。
int main(void)
{i / 7;
char * s =" / etc / filesystems" ;;
generic(i);
generic(s);
generic(" Hello");
generic(2048);
exit(0);
}
应该返回:
7
/ etc / filesystems
你好
2048
所以...
void generic(?? ?????)
我想看看sprintf,varargs等等。
有人能指出我正确的方向吗?
I would like to write a generic procedure that will take string or numeric
variables. I can not think of a way to make this more clear except to show
what I want. int main(void)
{
int i=7;
char *s="/etc/filesystems"; generic(i);
generic(s);
generic("Hello");
generic(2048);
exit(0);
} should return:
7
/etc/filesystems
Hello
2048 so... void generic( ???????)
{
???
} I have looked at sprintf, varargs etc. Could someone point me in the right direction?
对不起,但是没有正确的方向。类型是编译
时间概念,类型信息仅在
编译期间使用,并且仅隐含地包含在生成的
可执行文件中(即为什么使用某些机器指令和
而不是其他机器指令。所以在运行时类型信息不再存在
因此这样的通用函数是不可能的 -
没有附加到变量的附加类型信息一次
代码已被编译。
剩下的就是为什么你需要这样的通用
函数的问题(除此之外它有时会很高兴
他们) - 也许你的问题有不同的解决方案。
问候,Jens
- -
\ Jens Thoms Toerring ___ Je ***** ******@physik.fu-berlin.de
\ __________________________ http://www.toerring.de
" Walter L. Preuninger II" < WA ***** @ texramp.net>写在
新闻:l5 *********** @ 10.224.0.254:
"Walter L. Preuninger II" <wa*****@texramp.net> wrote in
news:l5***********@10.224.0.254:
我想写一个通用的程序,将采用字符串或
数字变量。除了展示我想要的东西外,我想不出办法让这个更清楚。
int main(无效)
{
int i = 7;
char * s =" / etc / filesystems" ;;
generic(i);
generic(s);
generic(" Hello");
通用(2048);
退出(0);
应返回:
7
/ etc / filesystems >你好
2048
所以...
无效通用(???????)
}
我看过sprintf,varargs等。
有人能指出我正确的方向吗?
I would like to write a generic procedure that will take string or
numeric variables. I can not think of a way to make this more clear
except to show what I want.
int main(void)
{
int i=7;
char *s="/etc/filesystems";
generic(i);
generic(s);
generic("Hello");
generic(2048);
exit(0);
}
should return:
7
/etc/filesystems
Hello
2048
so...
void generic( ???????)
{
???
}
I have looked at sprintf, varargs etc.
Could someone point me in the right direction?
>
讨厌说,但C ++是正确的方向。这是这些东西中的一个,在C中,这样做是相当笨拙的,容易出错,并且最终看起来不是很好.B $ b看起来非常好看。 />
-
- 马克 - >
-
Hate to say it, but C++ is the right direction for this. This is one of
those things, that in C, is rather clumsy to do, error prone, and doesn''t
look very nice in the end.
--
- Mark ->
--
" Walter L. Preuninger II" < WA ***** @ texramp.net> ó???Yéì/ó???Yéìá×??×?ó??è
$ b $bóì????Y ??:news:l5 *********** @ 10.224.0.254 ...
"Walter L. Preuninger II" <wa*****@texramp.net> ó???Yéì/ó???Yéìá × ??×?ó??è
óì???àY??: news:l5***********@10.224.0.254...
我想编写一个通用程序,它将采用字符串或数字
变量。除了显示我想要的东西之外,我想不出一种方法可以让它更清楚。
int main(void)
{
int i = 7;
char * s =" / etc / filesystems" ;;
generic(i);
generic(s);
generic(" Hello");
通用(2048);
退出(0);
应返回:
7
/ etc / filesystems >你好
2048
所以...
无效通用(???????)
}
我看过sprintf,varargs等。
有人能指出我正确的方向吗?
谢谢,
Walter
I would like to write a generic procedure that will take string or numeric
variables. I can not think of a way to make this more clear except to show
what I want.
int main(void)
{
int i=7;
char *s="/etc/filesystems";
generic(i);
generic(s);
generic("Hello");
generic(2048);
exit(0);
}
should return:
7
/etc/filesystems
Hello
2048
so...
void generic( ???????)
{
???
}
I have looked at sprintf, varargs etc.
Could someone point me in the right direction?
Thanks,
Walter
你需要实现多态或使用宏。如果你真的需要
这个设施,请尝试使用C ++。您可以在C中实现类似
的内容,请参阅 http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html
例如
-
vir
You''ll need to implement polymorphism or use macrosses. If you really need
this facilities, try using C++ instead. You CAN implement something like
this in C, see http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html
for example
--
vir
这篇关于确定变量类型的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!