问题
Yesterday in the interview one question was asked to me that can main
return struct
?
I have no idea can any one please tell me is it possible or not,if yes why?
回答1:
main
can only return an int
value in C (at least for hosted implementations).
回答2:
Section 5.1.2.2.1 of the C standard says no:
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;) or in some other implementation-defined manner
回答3:
No you can not
If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;10) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.
And you can not pass a struct to main, so the best is write this struct to a binary file. What do you wanna do?
Edit (for @effeffe):
#include <stdio.h>
struct st{float f;};
int main(void)
{
struct st x;
x.f = 3.14;
return x;
}
Returns
demo.c: In function ‘main’:
demo.c:9:5: error: incompatible types when returning type ‘struct st’ but ‘int’ was expected
回答4:
main
can only return int
in C/C++. This has to do with the ability to return a status code when a program has completed. 0
means success while 1
through 255
meant a particular error. Program would come would some sort manual in a text file or in print that would have the meaning of those errors so you could determine why your program couldn't run. This is assuming someone went through the effort of error checking and produced documentation.
回答5:
Since main is originally called from the operating system's program loader, it would have no idea what you're returning or what's supposed to be done with it.
Therefore, the convention is: return an integer. No matter what language, or what program, this provides a common interface for programs to communicate with the shell that launched it. This also means non-technical users who write scripts can use this integer to make decisions within their scripts.
The simplest way you can perform what you're asking is to save the structure to a file. This way any program that is aware of your structure's data can access the file.
回答6:
A joke answer:
A lot of people say no, but I think yes! (Sort of)
You could return a 32 bit pointer, which you then cast to an int, which points to a struct on the heap, assuming that int is of size 32 bits, and you have a 32 bit system so that the pointer is of size 32 bits also.
Then, rather dangerously, hope the operating system doesn't allocate that memory to another program to use, and hope that program doesn't overwrite it. If you use an os like Windows or Linux then my understanding is even if you don't free the memory, then the os will free it for you anyway. (Although you should always free your memory of course, else you be a dirty programmer!)
As far as I am aware another program could then declare a pointer to the location returned by your main(), and then read the contents of what was in the (now gone) struct byte by byte.
I think this should work? Time to test it! (This may go horribly wrong and lots of undefined things may happen.)
Don't take this answer too seriously. You will probably ruin your employment chances if you suggest it.
回答7:
As others have noted, if the return value of main
is not a type compatible with int
, its value is "unspecified". That means that a compiler could do just about anything it likes with the return value without violating the C standard, and it is not possible for a portable C program to return a structure type. On the other hand, nothing in the C standard requires that compiler vendors not document what will happen if code tries to have main
return a struct type, nor does it require that the compiler's documented behavior must never be useful. If an OS had a primaryReturnCode
and secondaryReturnCode
variables, and a compiler vendor wanted to specify that returning a struct containing two int
values would cause the first value to be stored in primaryReturnCode
and the second in secondaryReturnCode
, such a specification would be perfectly legitimate.
In C++, a program is required to have a main
function that returns int
. It would also be possible to have other functions with the name main
which return other things. I don't know whether a function which was named main
but had different parameters and return type from the startup main
function could appear in the same context as the startup one, but if nothing else functions called main
should be allowable within class scope.
来源:https://stackoverflow.com/questions/17262698/can-main-return-structure