Unit 5 - Pointers To Structure and Function
Unit 5 - Pointers To Structure and Function
A structure pointer is defined as the pointer which points to the address of the memory
block that stores a structure known as the structure pointer. Complex data structures
like Linked lists, trees, graphs, etc. are created with the help of structure pointers. The
structure pointer tells the address of a structure in memory by pointing the variable to
the structure variable.
There are two ways to access the members of the structure with the help of a structure pointer:
1. With the help of (*) asterisk or indirection operator and (.) dot operator.
2. With the help of ( -> ) Arrow operator.
#include <stdio.h>
struct person
int age;
float weight;
};
int main()
personPtr = &person1;
scanf("%f", &personPtr->weight);
printf("Displaying:\n");
return 0;
In this example, the address of person1 is stored in the personPtr pointer using personPtr =
&person1;
Now, you can access the members of person1 using the personPtr pointer.
By the way,
A function pointer in C is a variable that stores the address of a function. It allows you to refer to
a function by using its pointer and later call the function using that function pointer. This
provides flexibility in calling functions dynamically and can be useful in scenarios where you
want to select and call different functions based on certain conditions or requirements.
The function pointers in C are declared using the asterisk (*) operator to obtain the value saved
in an address.
A function pointer in C is useful to generate function calls to which they point. Hence, the
programmers can pass them as arguments. The function pointers enhance the code’s readability.
You can access many identical functions using a single line of code.
*ip is a pointer in the above declaration. It points to a function that returns an int value and also
accepts an integer value as an argument.
Functions Using Pointer Variables
In C, you can pass pointers as function arguments and return pointers from the function. If you
want to pass pointers in the function, you just need to declare the function parameter as a pointer
type.
#include <stdio.h>
int main()
increment(&numb);
return 0;
(*numb)++;
Output:
Referencing a function pointer in C refers to assigning the address of a function to the pointer
using the ‘&’ operator. On the other hand, Dereferencing a function pointer in C refers to
accessing a function and invoking it with arguments using the ‘*’ operator.
If you want to pass pointers to functions in C, you must define the function that accepts a pointer
parameter. Subsequently, you need to pass the address of the array or variable that you want to
work with.
#include <stdio.h>
*x = *y;
*y = temp;
int main()
{
int a = 50;
int b = 100;
swap(&a, &b);
return 0;
Output:
The above program shows that the swap function accepts two integer pointers as parameters.
Within the function, we’ve used these pointers to access and swap the variables’ values they
point to.
#include <stdio.h>
return a - b;
}
int main()
subPtr = subtract;
return 0;
Output:
In the above program, we declare a function pointer subPtr that points to a function that accepts
two integers as arguments and returns an integer. The syntax (*subPtr)(int, int) declares the
function pointer.
Within the main function, we assign the subtract function’s address to the function pointer with
the help of subPtr = subtract. So, subPtr now points to the subtract function. Subsequently, we
call the function via the function pointer by command subPtr(7, 4).
The below section discusses some common operations that you can accomplish using function
pointers:
Syntax:
return_type (*pointer_name)(parameter_list);
Example:
int (*funcPtr)(int, int); // This function pointer to a function accepts two int parameters and
returns int
Since each function has a unique memory address, you can use the function pointers in C to point
to the foremost executable code within a function body.
As seen from the example in the above figure, the function add() adds two integer numbers. The
function name points to the address of the function itself. Hence, we’ve used a function pointer
fptr that holds the address of the start of the function add(a, b) (the address is 1001 in the above
example).
Note: Before using a function pointer, you must declare it to inform the compiler about the type
of function the particular pointer can point to.
Calling a function using a pointer is the same as calling a function using the function’s name.
#include <stdio.h>
{
int sum = a + b;
int main()
return 0;
Output:
In the above program, the add function accepts two integers as arguments, computes their sum,
and displays the output. The declaration of addPtr function pointers happens with the parameter
types and return type that match the functions they would point to.
addPtr = add command assigns the add function’s address to the addPtr function pointer.
AddPtr(7, 8) calls the add function through the addPtr function pointer (with the input
arguments).