473,804 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting main() not allowed?

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,(ch ar *)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

Apr 25 '06 #1
13 1838
ballpointpenthi ef wrote:
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,(ch ar *)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

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.
Apr 25 '06 #2
"ballpointpenth ief" <Ma************ *@gmail.com> writes:
Have a look at this
[...] int main(void)
{ [...] strcat(this,(ch ar *)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?


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_Keit h) 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.
Apr 25 '06 #3

Keith Thompson wrote:
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>

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?

Apr 25 '06 #4
In article <11************ *********@j33g2 000cwa.googlegr oups.com>,
ballpointpenthi ef <Ma************ *@gmail.com> wrote:
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?


No.

It will typically work if int is "big enough" (which it often isn't on
modern systems), but even that is not guaranteed.

C99 provides an intptr_t integer type through which pointers can be
passed, but unfortunately it's optional.

-- Richard
Apr 26 '06 #5
ballpointpenthi ef wrote:
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,(ch ar *)main());
This will work assuming that the int value is a valid int representation
of a pointer.
printf("%s\n", this);
}
if (track == 6)
{
track = 7;
return (int)that;
This will work provided that the pointer converted to an int actually
fits. For a while, this has been the case on most popular
implementations , but with 64 bit systems this is quite likely not to
work. Pointers are 64 bits but ints are only 32 bits, so what do you
think will happen to the other 32 bits?
}
if (track == 7) return 0;
return 0;
}

I'm not really sure what my question is, but I'm quite suprised it
works.
Keep trying other systems and you will find conforming implementations
where it does not work.
Any other ridiculous things you can do with a recursive main?


Lots, However, I would not recommend any of them. The real challenge is
to find a sensible use for calling main recursively!
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 26 '06 #6
"ballpointpenth ief" <Ma************ *@gmail.com> writes:
Keith Thompson wrote:
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>

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?


Yes, I did miss that line. I also didn't take the time figure out the
program's control flow.

C99 6.3.2.3 p5-6:

An integer may be converted to any pointer type. Except as
previously specified, the result is implementation-defined, might
not be correctly aligned, might not point to an entity of the
referenced type, and might be a trap representation.

Any pointer type may be converted to an integer type. Except as
previously specified, the result is implementation-defined. If the
result cannot be represented in the integer type, the behavior is
undefined. The result need not be in the range of values of any
integer type.

--
Keith Thompson (The_Other_Keit h) 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.
Apr 26 '06 #7
Richard Tobin wrote:
...
C99 provides an intptr_t integer type through which pointers can be
passed, but unfortunately it's optional.


Why 'unfortunately' ? I have to wonder why it exists at all in C99!

--
Peter

Apr 26 '06 #8
"ballpointpenth ief" <Ma************ *@gmail.com> wrote:
Keith Thompson wrote:
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>

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?


Not only is it not portable, it is a very silly idea. Why on earth would
you want to torture main() so? What has it ever done to you?

Richard
Apr 26 '06 #9

Flash Gordon wrote:
ballpointpenthi ef wrote:


<snip>
Any other ridiculous things you can do with a recursive main?


Lots, However, I would not recommend any of them. The real challenge is
to find a sensible use for calling main recursively!


I was once writing a bytecode interpreter, where one of the legal
commands meant 'replace the currently running program with this string,
and run the string as if it were a program, with the command-line
arguments shifted left.' I implemented this with a static variable
holding the program, and when this command was reached it copied the
string into the program, and then executed something like
return main(argc-1, argv+1);
Of course, this could just be replaced by a loop, but it was not a
command that was frequently run (except by a bootstrap that always ran)
and I didn't want to special-case it in the startup code. (The static
variable initially held the bootstrap, and the bootstrap loaded the
file in argv[1] and ran it with that command. As a result, argv[1]
shifted onto argv[0] as a bonus, as it gave the filename of the program
now running). For a compiler that optimizes tail-recursion, this isn't
even very inefficient (a function calling itself in a 'return'
statement must be a good candidate for tail-recursion elimination!).

Apr 26 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3484
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
7
3683
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b; but it complains for the following lines
33
3669
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of serializing the object array). I can think of three ways to do this:
8
2436
by: Kris Jennings | last post by:
Hi, I am trying to create a new generic class and am having trouble casting a generic type to a specific type. For example, public class MyClass<Twhere T : MyItemClass, new() { public MyClass() { } public void AppendItem()
6
2610
by: Chameleon | last post by:
class A {} class B : public A {} void proceed(vector<A*&in); void main() { vector<B*a; proceed(a); // <-- error here! why? }
8
9373
by: Neha | last post by:
Hi all Its seems bit silly que but pleasee explan me why this error is coming ? and what will be the solution if i want void * to be intilaize by struct * and this code is puerly in C. -- #include <stdlib.h> #include <stdio.h>
5
5930
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style casting. from what I am reading, I should use reinterpret cast in this situation, is that correct? Why does static and dynamic casting fail me? // please excuse the windows types, it is necessary in this code, // but the question remains C++ related
9
2464
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I have a derived class object and then upcast it to its base class, which cast operator should I use? Is it static_cast, or, can I simply cast it implicitly without any operator? Does this upcasting remove the derived class portion of the object?...
4
1446
by: Rene | last post by:
If variance/covariance is not allowed in array of value types, why is the expression on the "Main" method run successfully (see snippet below)? I am missing something? Thank you. --------------------- class Program { enum Foo
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9591
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.