不允许施放main()? [英] Casting main() not allowed?

查看:73
本文介绍了不允许施放main()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这个


#include< stdio.h>

#include< string.h>

int track = 0;

char this [10] =" why";

char [10] =" not?" ;;

int main(无效)

{

track ++;

if(track< 5)

main();

if(track == 5)

{

strcat(this,(char *)main() );

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

}

if(track == 6)

{

track = 7;

return(int)that;

}

if(track == 7)返回0;

返回0;

}


我不太确定是什么我的问题是,但我很惊讶

的工作原理。

你可以用递归的主体做任何其他荒谬的事情吗?


马特

Have a look at this

#include <stdio.h>
#include <string.h>
int track = 0;
char this[10] = "why ";
char that[10] = "not?";
int main(void)
{
track++;
if (track < 5)
main();
if (track == 5)
{
strcat(this,(char *)main());
printf("%s\n", this);
}
if (track == 6)
{
track = 7;
return (int)that;
}
if (track == 7) return 0;
return 0;
}

I''m not really sure what my question is, but I''m quite suprised it
works.
Any other ridiculous things you can do with a recursive main?

Matt

推荐答案

ballpointpenthief写道:
ballpointpenthief wrote:
看看这个
<无线电通信/> #include< stdio.h>
#include< string.h>
int track = 0;
char this [10] =" why";
char [10] =" not?" ;;
int main(void)
{
track ++;
if(track< 5)
main();
if(track == 5)
{
strcat(this,(char *)main());
printf(" ;%s \ n",this);
}
if(track == 6)
{
track = 7;
return(int)that;
}
if(track == 7)返回0;
返回0;
}

我不确定我的问题是什么,但我很惊讶它
有效。
你可以用递归的主要做任何其他荒谬的事情吗?

Matt
Have a look at this

#include <stdio.h>
#include <string.h>
int track = 0;
char this[10] = "why ";
char that[10] = "not?";
int main(void)
{
track++;
if (track < 5)
main();
if (track == 5)
{
strcat(this,(char *)main());
printf("%s\n", this);
}
if (track == 6)
{
track = 7;
return (int)that;
}
if (track == 7) return 0;
return 0;
}

I''m not really sure what my question is, but I''m quite suprised it
works.
Any other ridiculous things you can do with a recursive main?

Matt



看起来它对任何递归函数都很好。 main()不是

与其他任何函数不同,除了它的第一个调用者是操作系统。


That looks like it''s good with any recursive function. main() is no
different than any other function, except that its first caller is the OS.


" ballpointpenthief" <马************* @ gmail.com>写道:
"ballpointpenthief" <Ma*************@gmail.com> writes:
看看这个

[...] int main(无效)
{
[...] strcat(这个,(char *)main());
[...]返回0;
}

我不确定我的问题是什么,但我很惊讶
有效。


当你说工作时你的意思是什么?


main是一个返回int的函数。你已经宣布了这一点

不带参数,所以main()是一个有效的调用;因为
返回的唯一值是0,表达式main()计算为int值0.


然后将此值转换为类型char * 。这是允许的,但

不便携;将整数转换为指针的结果是实现定义的
。将整数*常量* 0转换为

指针类型会产生空指针。将值为0的非常量整数

表达式转换为指针类型很可能会产生

a空指针,但它不是必需的(尽管在这一点上有一些

的分歧。


所以,你用最可能的空指针调用strcat()为

第二个参数。结果是未定义的行为,这是使用指针强制转换而不是*非常小心的常见结果。

强制转换通常会告诉编译器关闭并停止抱怨,

所以缺少一条警告信息(假设你没有得到一条)并不是令人惊讶的。

其他任何荒谬的你可以用递归的东西做什么?
Have a look at this
[...] int main(void)
{ [...] strcat(this,(char *)main()); [...] return 0;
}

I''m not really sure what my question is, but I''m quite suprised it
works.
What do you mean when you say it "works"?

main is a function that returns an int. You''ve declared it so it
takes no arguments, so main() is a valid call; since the only value it
returns is 0, the expression main() evaluates to the int value 0.

You then cast this value to type char*. This is allowed, but
non-portable; the result of converting an integer to a pointer is
implementation-defined. Converting an integer *constant* 0 to a
pointer type yields a null pointer. Converting a non-constant integer
expression with a value of 0 to a pointer type is very likely to yield
a null pointer, but it''s not required to (though there''s some
disagreement on this point).

So, you''re calling strcat() with what is most likely null pointer as
its second argument. The result is undefined behavior, which is a
common result of using pointer casts without being *very* careful.
Casting typically tells the compiler to shut up and stop complaining,
so the lack of a warning message (assuming you didn''t get one) isn''t
surprising.
Any other ridiculous things you can do with a recursive
main?




有很多不好的C程序可以编写。唉,

实际存在的太多了;我恐怕没时间了。

列举所有其他人。


-

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

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

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



There are infinitely many bad C programs that can be written. Alas,
too many of them actually exist; I''m afraid I don''t have time to
enumerate all the others.

--
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.




Keith Thompson写道:

Keith Thompson wrote:
然后你投了这个键入char *的值。这是允许的,但是不便携;将整数转换为指针的结果是实现定义的。
所以,你用最可能的空指针调用strcat()作为它的第二个参数。结果是未定义的行为....
You then cast this value to type char*. This is allowed, but
non-portable; the result of converting an integer to a pointer is
implementation-defined. So, you''re calling strcat() with what is most likely null pointer as
its second argument. The result is undefined behavior....



< snip>


我认为你必须撇去这条线...

return(int)that;

我返回的指针转换为整数。 (虽然我是

不确定这是否真的适合int。)

可移植吗?


<snip>

I think you must have skimmed the line...
return (int)that;
Where I am returning a pointer converted to an integer. (Although I''m
not sure whether this would neccesarily fit in an int.)
Is that portable?


这篇关于不允许施放main()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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