why can't we declare functions inside a structure?

13,617

Solution 1

Well that is the fundamental difference between C and C++ (works in C++). C++ supports classes (and in C++ a struct is a special case of a class), and C does not.

In C you would implement the class as a structure with functions that take a this pointer explicitly, which is essentially what C++ does under the hood. Coupled with a sensible naming convention so you know what functions belong to which classes (again something C++ does under then hood with name-mangling), you get close to object-based if not object-oriented programming. For example:

typedef struct temp
{
    int a;

} classTemp ;

void classTemp_GetData( classTemp* pThis )
{
    printf( "Enter value of a : " );
    scanf( "%d", &(pThis->a) );
}

classTemp T ;

int main()
{
    classTemp_GetData( &T );
}

However, as you can see without language support for classes, implementing then can become tiresome.

In C, the functions and data structures are more or less bare; the language gives a minimum of support for combining data structures together, and none at all (directly) for including functions with those data structures.

The purpose of C is to have a language that translates as directly as possible into machine code, more like a portable assembly language than a higher-level language such as C++ (not that C++ is all that high-level). C let's you get very close to the machine, getting into details that most languages abstract away; the down side of this is that you have to get close to the machine in C to use the language to its utmost. It takes a completely different approach to programming from C++, something that the surface similarities between them hide.

Check out here for more info (wonderful discussions there).


P.S.: You can also accomplish the functionality by using function pointers, i.e. have a pointer to a function (as a variable) inside the struct.

For example:

#include <stdio.h>

struct t {
    int a;
    void (*fun) (int * a);        // <-- function pointers
} ;

void get_a (int * a) {
    printf (" input : ");
    scanf ("%d", a);
}

int main () {
    struct t test;
    test.a = 0;

    printf ("a (before): %d\n", test.a);
    test.fun = get_a;
    test.fun(&test.a);
    printf ("a (after ): %d\n", test.a);

    return 0;
}

Solution 2

WHY function can not be declared inside structure?

Because C standard does not allow to declare function/method inside a structure. C is not object-oriented language.

6.7.2.1 Structure and union specifiers:

A structure or union shall not contain a member with incomplete or function type(hence, a structure shall not contain an instance of itself, but may contain a pointer to an instance of itself), except that the last member of a structure with more than one named member may have incomplete array type; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array.

Share:
13,617
zee
Author by

zee

Updated on June 04, 2022

Comments

  • zee
    zee almost 2 years

    I read plenty of questions regarding

    declaration of functions inside structure?

    But I did not get a satisfactory answer.

    My question is not about whether functions can be declared inside a structure or not?

    Instead, my question is

    WHY function can not be declared inside structure?