0% found this document useful (0 votes)
23 views9 pages

Unit 5 - Pointers To Structure and Function

The document explains structure pointers and function pointers in C programming, detailing how to define and use them to access structure members and call functions dynamically. It includes examples demonstrating the use of pointers for structures, function calls, and pointer operations. Additionally, it highlights the advantages of using function pointers for code flexibility and readability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views9 pages

Unit 5 - Pointers To Structure and Function

The document explains structure pointers and function pointers in C programming, detailing how to define and use them to access structure members and call functions dynamically. It includes examples demonstrating the use of pointers for structures, function calls, and pointer operations. Additionally, it highlights the advantages of using function pointers for code flexibility and readability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Structure Pointer in C-

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.

Accessing the Structure Member with the Help of Pointers

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.

Example: Access members using Pointer

To access members of a structure using pointers, we use the  operator.

#include <stdio.h>

struct person

int age;

float weight;

};

int main()

struct person *personPtr, person1;

personPtr = &person1;

printf("Enter age: ");


scanf("%d", &personPtr->age);

printf("Enter weight: ");

scanf("%f", &personPtr->weight);

printf("Displaying:\n");

printf("Age: %d\n", personPtr->age);

printf("weight: %f", personPtr->weight);

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,

personPtr->age is equivalent to (*personPtr).age

personPtr->weight is equivalent to (*personPtr).weight


Function Pointer in C

Understanding Function Pointers in C

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.

Advantages of Function Pointers

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.

Other prominent advantages of function pointer in C include passing functions as parameters to


other functions. Therefore, you can create generic functions which can be used with any function
provided that it possesses the right signature.

Syntax of Function Pointer in C

Here is the function pointer syntax in C.

return type (*ptr_name)(type1, type2...);

Let’s look at an example:

int (*ip) (int);

*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.

Here’s a C program demonstrating functions using pointer variables.

#include <stdio.h>

void increment(int *numb);

int main()

int numb = 10;

printf("The number before increment is: num = %d\n", numb);

increment(&numb);

printf("The output after increment is: num = %d\n", numb);

return 0;

void increment(int *numb)

(*numb)++;

Output:

The number before increment is: num = 10

The output after increment is: num = 11


In the above program, the increment function increments an integer’s value using a pointer. The
numb variable is declared and initialized to 10. Subsequently, we call the increment function and
pass the address of numb variable. Lastly, the value of numb is printed at the output.

Referencing and Dereferencing of Function Pointer in C

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.

Program to pass pointers with functions in C

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.

Here's a program that demonstrates passing pointers with functions:

#include <stdio.h>

// C program to swap two integers through pointers

void swap(int* x, int* y)

int temp = *x;

*x = *y;

*y = temp;

int main()

{
int a = 50;

int b = 100;

printf("The numbers before swap: a = %d, b = %d\n", a, b);

swap(&a, &b);

printf("The numbers after swap: a = %d, b = %d\n", a, b);

return 0;

Output:

The numbers before swap: a = 50, b = 100

The numbers after swap: a = 100, b = 50

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.

Example for Function Pointer in C

Here’s an example program that demonstrates function pointer in C.

#include <stdio.h>

// The following function subtracts two numbers

int subtract(int a, int b)

return a - b;

}
int main()

// The command declares function pointers

int (*subPtr)(int, int);

subPtr = subtract;

printf("The subtraction of two numbers: %d\n", subPtr(7, 4));

return 0;

Output:

The subtraction of two numbers: 3

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).

Function Pointer Operations

The below section discusses some common operations that you can accomplish using function
pointers:

i) Declaring a function pointer:

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

ii) Assigning a function address to a function pointer:

sumPtr = add; // Assigns the address of a function to a function pointer.

Declaring a Function Pointer in C

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.

Declaration of function pointer in C

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 Functions Using Pointers

Calling a function using a pointer is the same as calling a function using the function’s name.

The following program demonstrates calling functions using pointers.

#include <stdio.h>

void add(int a, int b)

{
int sum = a + b;

printf("The addition of %d and %d is: %d\n", a, b, sum);

int main()

void (*addPtr)(int, int); // Function pointer declaration

addPtr = add; // Assigning address of add function

addPtr(7, 8); // Calling the function using function pointer

return 0;

Output:

The addition of 7 and 8 is: 15

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).

You might also like