Can We Declare Function inside Structure of C Programming?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

A structure in C allows us to store multiple variables of different or the same data types together. In C, the struct keyword is used to create a structure. A structure is a user-defined data type used to store a group of data of different or the same data types. The syntax is,

The following structure can be used to represent an animal,

The variable inside the structure can be accessed by creating a structure variable and using the . operator. In the above example, the structure variable is dog.If we want to set the age of the animal to 5, we can use the following code,

The structures are only used to store data and we can't declare a function inside a structure. There are several reasons why we can't declare a function inside C,

  • It is not supported by the compiler used by the C programming language. The possible reasons are, that the C programming language was created in 1972, and influenced by a very low-level language called the assembly language.
  • C is not an object-oriented programming language. There is no provision for creating objects with their data and properties to modify that data(functions).
  • It doesn't provide any features to identify that a function belongs to a particular structure variable or there is no certainty that the function can access the variable in the structure.

Consider the following hypothetical example in which a function is declared inside a structure,

A structure variable is a single instance of the structure with its data variables. If there is a function inside a structure then that function must access or modify the data variables present only in that structure variable. From the above example, we can see the main problem is that the percentage that is being modified by the function must belong to the same variable of structure as the function, but there is no way to identify that the calculate_percentage() can access only variable belonging to the variable of structure.

Let us consider a variable of the structure test called maths,

In the above example, it is certain that the percentage in maths.percentage belongs to maths, but we can't say that the percentage present inside the function calculate_percentage() belongs to maths.

In programming languages like C++, this is done by the this pointer which points to the current structure or class and is a default parameter for every function present inside a structure or class.

A function calculate_percentage()inside a struct or class in C++ will behave like,

Thus the this keyword identifies the structure or class for the function.

  • Another reason is, that there is no absolute necessity for a function to be declared inside a structure in C because any changes that can be performed by a function inside a structure can also be performed by a function outside the structure. But the problem with this method is that it gives direct access to the structure and inhibits privacy. The function calculate_percentage() discussed above can be implemented without including it inside a structure by passing the structure as a parameter.

This is indeed less effective than having a function inside a structure like C++, but C was designed so that it could be processed with a relatively simple compilation system. The main purpose of the C programming language is to have a language that is very closer to machine code and acts as a portable assembly language. than a higher-level language.

Even though we can't declare functions inside a structure, we can declare a function pointer that point to a function inside a structure. A function pointer is a variable that is used to store the address in which the code for a function is present. The syntax for creating a function pointer is,

The function pointer must be assigned to a function to point to that function.

For example,

These function pointers are declared inside structures and assigned to a function that is declared outside the structure. This approach can be used as an alternative to declaring a function inside a structure. Even though this method can be a good solution for the inability to declare a function inside a structure, it compromises the structure's privacy since the actual function is still declared outside of the structure.

Consider the following example in which we have calculated the percentage

The output will be,

  • Declaring a function pointer inside doesn't change the fact that functions can't be declared inside a structure.
  • From the code, we can understand that the function pointer also doesn't have access to the variables in the same instance of the structure. To solve this we have provided each variable of the structure as a parameter to the function call.
  • There is less memory usage for function pointers compared to real functions.

Example and Usage of Structure

  • Structure forms the base for object-oriented programming and has many use cases and real-world applications.
  • Structures are user-defined data types.

Let us consider a simple use case in which we need to save the records of students. In such a case a structure can be used to hold the record of a student and an array of structures can be used to hold the records of a class of students. A student may have a name, roll number, and an associated department. These details can be represented in a structure as,

A single student can be represented by an instance of a structure and values can be modified or accessed by the . operator.

A structure can use the {} way of initialization to initialize a structure with values.

  • A structure can be initialized in different ways and variables inside the structure can also be accessed using different ways.
  • A structure can also have pointers pointing to itself or other structures. This is used in creating different data structures.
  • A structure can also be declared as a nested structure.

Each of the above points is more elaborated and explained with examples in this article

Computer systems use structures for the following uses,

  • Hiding a file from the directory
  • Interacting with the mouse
  • Clearing the contents of the screen
  • Drawing any graphic shape on the screen

Operating systems use structures to store data for internal computational operations.

Conclusion

  • A structure is a user-defined data type to store data of different or same data types.
  • C doesn't allow us to declare a function inside a structure. This is mainly because C is a simple language and doesn't support object-oriented programming.
  • Function pointers can be stored inside a structure.