Member Avatar for I_m_rude

hi,

int fun(int(*)())
{
     .......
     .......
     ......
}

int main()
{
     .......
     ......
     .......
     fun(main);

     ........
     ........
}

my question is that how can we use "main" (only name without parenthesis) as the pointer to a function ? because
main() is a function. I am not getting it how is it true ?

Secondly,
Can anyone please explain what exactly "preprocessor" and "preprocessor dirctives" mean ? I know directives are like #if...#elif...#endif etc... but what exactly these 2 terms mean ? please throw some light.

thanks in advance.

Here is a good wiki article about preprocessor directives.

As for your program. This may compile, but it won't work because it's recursive. main() should never be called by your program.

int fun( int foo() )
{

    return foo();
}

int main()
{
    fun(main);
}
Member Avatar for I_m_rude

It's just a snippet. break statement will defintely be there. answer my question please!

my question is that how can we use "main" (only name without parenthesis) as the pointer to a function ? because
main() is a function. I am not getting it how is it true ?

It's syntactic convenience based on the fact that you can only do two things with a function: call it or take its address. If you're not calling a function with the () operator then you must be taking its address, and thus the address-of operator isn't necessary.

So fun(main) is equivalent to fun(&main).

You cannot use main as a function pointer. It is undefined behaviour.

#include <stdio.h>
#include <stdlib.h>

int i;
int func(int nargs, char **args, int (*function)(int length, char** args)) {
    function(nargs, args);
}

int main(int nargs, char **args) {
    func(nargs, args, main);
    for (i = 0; i < nargs; i++) printf("%s", args[i]);
}

On my linux machine causes seg fault.
Operating system is supposed to call main, not any user program. So even through it's a function, it may actually be different from any user-defined function. For example it may pass function arguments through registers instead of the stack.

You cannot use main as a function pointer. It is undefined behaviour.

Only in C++, C has no such restriction. Though it's commonly accepted that taking main()'s address or calling it recursively in C is stupid. C11 may have tightened up the rules for usage of main though, I'm not as familiar C11 as I am with C99 and previous revisions.

Only in C++, C has no such restriction. Though it's commonly accepted that taking main()'s address or calling it recursively in C is stupid. C11 may have tightened up the rules for usage of main though, I'm not as familiar C11 as I am with C99 and previous revisions.

I do not see how it can ever be useful for programming. Everything that is possible to do with calling main recursively can also be achieved without it, in a much nicer way. As you said, in C, a lot of stupid things can be technically legal but in reality not making sense.
And even through it may be legal in standards, but when compiled in gcc it gives a segmant fault.

And even through it may be legal in standards, but when compiled in gcc it gives a segmant fault.

Are you compiling with strict conformance as C? This might just be one of those obscure areas where gcc isn't conformant yet nobody cares enough to fix it.

Never mind, it compiles. I had a small mistake in my previous code :(
This prints the arguments passed to the function 5 times:

#include <stdio.h>
#include <stdlib.h>
int i;
int d = 0;

int func(int nargs, char **args, int (*function)(int length, char** args)) {
    d++;
    if (d < 5) function(nargs, args);

}

int main(int nargs, char **args) {
    for(i = 0; i < nargs; i++) printf("\n%s", args[i]);
    func(nargs, args, main);
}

It works in g++ c++11 mode too.

Member Avatar for I_m_rude

hey, On this thread you both are distracted from the topic. please tell the answers for which this thread is started

please tell the answers for which this thread is started

Please explain your remaining confusion. Both Ancient Dragon and I collectively answered both of your questions.

Member Avatar for I_m_rude

if anyone asks me, define preprocessor nd preprocessor directives. then
for preprocessor, I will say "it is a seprate program which is called by the compiler as the first step of the compilation which is basically neccassary for the macro substituion, including files,etc." is it right ?

And secondly, can you tell the same/typical definition for the preprocessor directives? thanks.

Member Avatar for I_m_rude

@decptikon will you please tell ?

Please tell what? Read the link posted by Ancient Dragon in the first reply. It answers your questions about the preprocessor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.