As the heading goes, Is it possible for the main function return a pointer? If yes then where would be useful? Thanks!
-
possible duplicate? http://stackoverflow.com/q/204476/884412 – penelope Jun 11 '12 at 12:20
-
3Where would you imagine this to be useful? – Fred Foo Jun 11 '12 at 12:24
-
@larsmans yeah right., even I stumbled when I was asked this by one of my friend. – Shash Jun 11 '12 at 12:38
3 Answers
Only a return type of int
is blessed by the standard:
5.1.2.2.1 Program startup
1 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;10) or in some other implementation-defined manner.
C11 draft, April 12, 2011
Everything else is up to your compiler and therefore not a C-question anymore.

- 38,570
- 8
- 95
- 130
No. main()
returns an int
in standard C. The interpretation of this return value is a matter for the surrounding runtime environment. If you know exactly what you're doing, in some kind of specialized situation where you know that the environment is going to interpret the value in a certain way, then you can cast a pointer to an int. But that's nasty.
And be aware that a pointer to dynamically-allocated memory is (probably) meaningless after the program exits anyway! It would only make sense to return a pointer to a fixed address, e.g. a hardware register in some embedded environment.

- 60,055
- 21
- 138
- 179
-
Or say a pointer to a block of shared memory owned by a different process, assuming your OS maps shared memory to the same location in every process space. Or maybe a pointer to something in kernel space, or the video framebuffer. – Rup Jun 11 '12 at 12:33
-
In those cases it would almost always make more sense to return an offset from some base that the receiving end already knows... – R.. GitHub STOP HELPING ICE Jun 11 '12 at 13:52
-
I quite agree. I was clutching at straws trying to think of ways in which it might be useful. :) – Graham Borland Jun 11 '12 at 14:04
Completing phresnel's answer, and answering whether it would be useful: since each process has its own address-space, even if your main()
returns an integer value that you'd use as an address, what would be the point since the returned pointer is only consistent to the just exited process's address-space...

- 4,095
- 1
- 26
- 27
-
"since each process has its own address-space" - that's outside the scope of C, and depends on your CPU architecture as well as operating system. – snemarch Jun 11 '12 at 12:35
-
In the abstract machine, it has its own address space, i.e. even if address space is shared, there's no way within the C language to determine that it is. – R.. GitHub STOP HELPING ICE Jun 11 '12 at 13:53