Pointers to Structure in C Programming

Arrays will always hold elements of same datatype. i.e. ; its elements will be always integer, float, double, string etc; but not the combination of different variables. But if we need to have combination of datatypes in a same variable, then we use structures. Structures are special type of variable which holds multiple variables of same group but will have different datatypes for those variables.

struct person {
char first_name [30];
char last_name [15];
int age;
char is_employed;
};

Suppose we have a function which displays the first name and last name passed to it from the structure. What we will do to pass first and last name from the structure? One method is to create two string variables, assign the first and last name from the structure to them. Then call the function by passing these two string variables. So far, it is simple. Suppose latter the requirement changes to display the age too! What do we do now? Change the program everywhere – add another integer variable, assign the age from structure and pass it to function. In addition to this, modify the function too for accepting another integer variable and display it. This requires lot of changes in the code. This is not acceptable as structure can have large number of variables.

If we can pass whole structure to the function, then it reduces lot many code changes in the main program. We only need to take care of displaying the required variables in the function. But how do we pass the structure to a function? When we pass the structure, their values also have to be passed. Only then we can print them in the function. This can be achieved easily by using pointers on structures. We can create a pointer to point to a structure similar to the array. But here accessing the structure variables differ from accessing the array elements. When we create a pointer to structure, it points to the beginning of the structure variable. Then we can access the variables within structure similar to any other normal structure.

Below program demonstrates declaration, assigning and accessing methods of structure pointer.  A structure pointer structPtr is created to point to structure structPer. Now the pointer is pointing to the structure and hence it will have the address of the structure. When we call the function, we pass the structure to the function by simply passing the address that it contains. But the function definition captures the value of the structure (we have *p in function definition to grab its values whereas while calling the function we do not have any ‘*’ or ‘&’. Only the value present in pointer – address is passed as argument). Hence the values of the structure that we are passing are available inside the function. Please note the different notations using ‘→’ and ‘*’ is used for accessing the structure variables.

Now if we need to display age or is_employed in the function, then we have to add another printf line in the code. This makes the code simpler.

#include <stdio.h>
#include <string.h>

struct person {
	char first_name[30];
	char last_name[15];
	int age;
	char is_employed;
};

void display_names(struct person *p) {// structure pointer as an argument
	// Observe the different notations used to access the structure pointer variables
	printf("First name in the structure is : %s \n", p->first_name);
	printf("Last name in the structure is : %s \n", (*p).last_name);
}

int main()
{
	struct person structPer;
	struct person *structPtr; // declare a structure pointer

	// Assign the values to the structure
	strcpy(structPer.first_name, "Yashvanth");
	strcpy(structPer.last_name, "Kanetkar");
	structPer.age = 50;
	structPer.is_employed = 'Y';

	structPtr = &structPer; // Points to the structure structPer

	display_names(structPtr); // Call the function by passing the structure pointer
	return 0;
}

We can define pointers to union in the same way we did for structures. Members of the unions are also accessed in the same way we accessed structures.

Translate »