Why Do We Need Pointers to Structures?
Pointers to structures are essential for several reasons:
- Efficient Memory Usage: When we pass a struct to a function, a pointer allows us to avoid copying the entire struct, which is especially useful for large structures. By passing a pointer, we directly work with the structure's memory address.
- Dynamic Memory Allocation: Pointers allow dynamic allocation of memory for structs, which is crucial for creating flexible data structures like linked lists, trees, and graphs that may need to grow or shrink at runtime.
- Simplified Data Access and Manipulation: With struct pointers, we can modify data within a function without returning anything, as changes are made directly in memory. This is beneficial in situations where multiple parts of a program need access to the same data.
- Building Complex Data Structures: Pointers to structs are key to implementing linked lists, trees, and other advanced data structures where each node or element is a struct containing data and pointers to other structs.
Pointers
The Pointer in C programming is a variable that can store the address of another variable. A pointer also can be used to consult any other pointer function. A pointer may be incremented/decremented, i.e., to point to the next/ previous memory location. The reason behind a pointer is to keep memory space and achieve faster execution time.
Declaring a Pointer
Like variables, pointers in C programming should be declared earlier than they can be used for your program. Pointers can be named anything that you want as long as they obey C’s naming rules. A pointer declaration has the following form.
data_type * pointer_variable_name;
here,
- data_type is the pointer’s base type of C’s variable types and suggests the type of the variable that the pointer points to.
- The asterisk (*: the identical asterisk used for multiplication) that’s indirection operator declares a pointer.
Initialize a pointer
After declaring a pointer, we initialize it like standard variables with a variable address. If pointers in C programming aren’t uninitialized and used inside the program, the results are unpredictable and potentially disastrous.
To get the address of a variable, we use the ampersand (&)operator, placed before the name of a variable whose address we need. Pointer initialization is performed with the following syntax.
Syntax
pointer = &variable;
Example
C
#include <stdio.h>
int main()
{
int a=10; //variable declaration
int *p; //pointer variable declaration
p=&a; //store address of variable a in pointer p
printf("The address stored in a variable p is:%x\n",p); //accessing the address
printf("The value stored in a variable p is:%d\n",*p); //accessing the value
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
The address stored in a variable p is:60ff08
The value stored in a variable p is:10
In the above example, we performed Pointer initialization. And show how can stored address and value in a variable.
You can also read about the dynamic arrays in c.
Struct
C struct, short for C structure, is a user-defined data type in C. It allows users to mix data items of possibly different data types below a single name.
C structs are special from arrays because arrays hold data of similar data types. On the other hand, the C struct can store multiple data types.
Every element inside the structure is called a member.
Syntax
A structure is described through the usage of the struct keyword. A structure is a likely collection of primary data types and other structures
- The structure_name holds the name we want to present to our structure.
- data_type variable is the C variables of different data types like int, char, float, etc.
struct structure_name
{
//data_type variable 1
//data_type variable 2
//data_type variable 3
...
};
Defining a structure
The code under initializes a structure employee having three member variables:
- name: An array of char types.
- Age: A variable of int type.
- Salary: A variable of float type.
struct Employee
{
char name[50];
int age;
float salary;
};
Initializing a structure
Structures can be assigned values while they’re initialized.
Before initialization
C
struct Employee
{
char name[50];
int age;
float salary;
};
int main() {
struct Employee e1 = {"John", 32, 4200};
//accessing the values in the variable
printf("Name: %s\n", e1.name);
printf("Age: %d\n", e1.age);
printf("Salary: %f\n", e1.salary);
}

You can also try this code with Online C Compiler
Run Code
Output
Name: John
Age: 32
Salary: 4200.000000
In this example, We performed before initialization of the structure.
After initialization
C
struct Employee
{
char name[50];
int age;
float salary;
};
int main()
{
struct Employee e1;
strcpy(e1.name, "John");
e1.age = 32;
e1.salary = 4200;
//accessing the values in the variable
printf("Name: %s\n", e1.name);
printf("Age: %d\n", e1.age);
printf("Salary: %f\n", e1.salary);
}

You can also try this code with Online C Compiler
Run Code
Output
Name: John
Age: 32
Salary: 4200.000000
In this example, We performed after initialization of the structure.
Accessing a Structure
The values in a structure variable can be accessed in the following manner:
R
struct Employee
{
char name[50];
int age;
float salary;
};
int main() {
struct Employee e1 = {"John", 32, 4200};
//accessing the values in the variable
printf("Name: %s\n", e1.name);
printf("Age: %d\n", e1.age);
printf("Salary: %f\n", e1.salary);
}

You can also try this code with Online R Compiler
Run Code
Output
Name: John
Age: 32
Salary: 4200.000000
This example shows how the values in a structure variable can be accessed.
Dynamic memory allocation of structs
Before continuing this section, we advocate you check C dynamic memory allocation.
Sometimes, the number of struct variables you declare may be insufficient. You could need to allocate memory during run-time. Here’s how we can achieve this in C programming.
Example
C
#include <stdio.h>
#include <stdlib.h>
struct person {
int age;
float weight;
char name[30];
};
int main()
{
struct person *ptr;
int i, n;
printf("Enter the number of persons: ");
scanf("%d", &n);
// allocating memory for n numbers of struct person
ptr = (struct person*) malloc(n * sizeof(struct person));
for(i = 0; i < n; ++i)
{
printf("Enter first name and age respectively: ");
// To access members of 1st struct person,
// ptr->name and ptr->age is used
// To access members of 2nd struct person,
// (ptr+1)->name and (ptr+1)->age is used
scanf("%s %d", (ptr+i)->name, &(ptr+i)->age);
}
printf("Displaying Information:\n");
for(i = 0; i < n; ++i)
printf("Name: %s\tAge: %d\n", (ptr+i)->name, (ptr+i)->age);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Enter the number of persons: 2
Enter first name and age respectively: Harry 24
Enter first name and age respectively: Gary 32
Displaying Information:
Name: Harry Age: 24
Name: Gary Age: 32
This example shows how the memory allocated of struct and how it is work.
Pointers to Structures
We have already learned a pointer is a variable that points to the address of every other variable of any data type like int, char, float, etc. similarly, we can have a pointer to structures, in which a pointer variable can point to the address of a structure variable. Here we know how we can declare a pointer to a structure variable.
Example
struct dog
{
char name[20];
char breed[20];
int age;
char color[20];
};
struct dog spike;
// declaring a pointer to a structure of the type struct dog
struct dog *ptr_dog
This declares a pointer ptr_dog that can store the address of the variable of type struct dog. We can now assign the address of variable spike to ptr_dog using & operator.
ptr_dog = &spike;
Now ptr_dog points to the structure variable spike.
Frequently Asked Questions
How Big is a Struct Pointer?
The size of a struct pointer is typically 4 bytes on 32-bit systems and 8 bytes on 64-bit systems, regardless of the struct's size.
What is the Next Pointer in a Struct?
The next pointer in a struct is a pointer that usually points to another struct of the same type, often used in linked lists to connect nodes.
What is the size of a C structure.?
Individually calculate the sizes of each member of a structure and make a total to get the Full size of a structure.
C Structure or User-defined data type is also called.?
Derived data type, Secondary data type, Aggregate data type
Conclusion
In this article, we have extensively discussed Struct and pointers topics and their implementation. We learned about initializing Struct and pointers and dynamically allocated memory. We can hope that this blog has helped you enhance your knowledge regarding Struct and pointers and if you want to learn extra, check out our articles on interview prepration here.