Why the user-defined function needs to be before the main function for a function pointer to work

Debojit Acharjee 455 Reputation points
2023-07-26T13:56:14.3233333+00:00

Why the user-defined function needs to be before the main function for a function pointer to work.

This one works:

#include <stdio.h>

int add(int a, int b)
{
    int c;

    c = a + b;

    return c;
}

int main()
{
    int a, b, sum;
    int (*ab)(int, int) = &add; // Declaration and initialization of function pointer

    sum = (*ab)(1, 2); // Dereferencing of function pointer

    printf("1 + 2 = %d", sum);

    return 0;
}

But why this one doesn't work:

#include <stdio.h>

int main()
{
    int a, b, sum;
    int (*ab)(int, int) = &add; // Declaration and initialization of function pointer

    sum = (*ab)(1, 2); // Dereferencing of function pointer

    printf("1 + 2 = %d", sum);

    return 0;
}

#include <stdio.h>

int add(int a, int b)
{
    int c;

    c = a + b;

    return c;
}

int main()
{
    int a, b, sum;
    int (*ab)(int, int) = &add; // Declaration and initialization of function pointer

    sum = (*ab)(1, 2); // Dereferencing of function pointer

    printf("1 + 2 = %d", sum);

    return 0;
}

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 82,146 Reputation points Volunteer Moderator
    2023-07-26T15:48:32.36+00:00

    C is single pass compiler and does not support forward references. That means all variables and methods must be declared before use.

    note: you add a method declarations at the start of the file and the implementation later to allow any order of method declarations. This is considered best practice.

    this also why .h files are required for external symbols.


  2. Minxin Yu 13,506 Reputation points Microsoft External Staff
    2023-07-27T07:04:22.2466667+00:00

    Hi,
    From Function Declarations and Definitions
    Both function and variable declarations can appear inside or outside a function definition.

    add() means that you can pass any number of arguments.

    Compilation may fail if parameters are added. It depends on your compiler.

    User's image

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.