Struct Pointer in C
A struct pointer is a pointer that holds the address of a structure rather than the structure itself. It is useful because it allows passing the address of a struct in functions rather than passing the struct itself, which consumes less memory space.
In this lecture, we will prove that a struct pointer consumes less memory than a struct without a pointer.
Example To Prove struct pointer consumes less memory
Let’s assume our Book struct has the following structure:
struct Book {
char title[100]; // 100 bytes char author[100]; // 100 bytes int pages; // 4 bytes (on a 32-bit or 64-bit system)
};
Each field in Book consumes a specific amount of memory:
- title: 100 bytes
- author: 100 bytes
- pages: 4 bytes
Total Memory Size of Book: 100 + 100 + 4 = 204 bytes
1. Struct Without a Pointer
When you struct without a pointer, you pass a struct by value to a function, then a complete copy of the struct is created in memory. This means each field in the struct (such as title, author, and pages) is duplicated.
important: changes made to the struct inside the function won’t affect the original struct outside the function.
Code Example:
#include <stdio.h> struct Book { char title[100]; char author[100]; int pages; }; // Function to print Book details - struct passed by value void printBookByValue(struct Book b) { printf("Book Title: %s\n", b.title); printf("Author: %s\n", b.author); b.pages = 111; printf("Pages: %d\n", b.pages); } int main() { struct Book book1 = {"C Programming Language", "Brian W. and Dennis", 279}; printBookByValue(book1); // Passing struct by value (copy of the struct is passed) printf("Pages: %d\n", book1.pages); // Original `pages` remains unchanged return 0; } Output
Book Title: C Programming Language
Author: Brian W. and Dennis
Pages: 111
Pages: 279
Memory Consumption Without Struct Pointer
Original Struct (book1): This struct exists in memory with all its fields.
Copy Creation in Function Call:
-
- When printBookByValue(book1); is called, a new copy of book1 is created in memory for the parameter b.
- This duplicate occupies the same amount of memory as book1.
Process:
-
- The fields (title, author, and pages) are copied into the duplicate struct b.
- Any changes to “b” inside printBookByValue do not affect the original book1, as they are separate in memory.
Memory Layout (Passing by Value)
Struct Book | |||||
book1 (204 bytes) | Duplicate b (204 bytes) | ||||
title (100 bytes)
|
title (100 bytes)
|
This results in a total memory usage of 204 + 204 = 408 bytes. In this example, both “book1” and “b” occupy separate memory spaces, doubling the required memory for the struct during the function call.
2. Struct with a Pointer
When you pass a struct by pointer, only the memory address of the struct is copied, rather than duplicating the entire struct. This makes the function more memory-efficient, especially with large structs.
Code Example:
#include <stdio.h> struct Book { char title[100]; char author[100]; int pages; }; // Function to print Book details - struct passed by pointer void printBookByPointer(struct Book *b) { printf("Book Title: %s\n", b->title); printf("Author: %s\n", b->author); b->pages = 111; // it will change the original content in struct. printf("Pages: %d\n", b->pages); } int main() { struct Book book1 = {"C Programming Language", "Brian and Dennis", 279}; // pass values to structure printBookByPointer(&book1); // Passing struct by pointer (only address is copied) printf("Pages: %d\n", book1.pages); // The expected result should be 279, but it is currently 111 due to an update made through the structure pointer. return 0; } Output: Book Title: C Programming Language Author: Brian and Dennis Pages: 111 Pages: 111
Important: Note that function and structure are two different concepts; changing the function updates the original content in the structure. This is because we are accessing this function using a structure pointer. |
Memory Consumption Without Struct Pointer
Original Struct (book1): This struct exists in memory with all its fields, just like before.
Pointer Copy in Function Call: When printBookByPointer(&book1); is called, only the address of book1 is copied to the parameter “b”. Instead of duplicating the entire struct, the function simply uses a pointer (address) to access book1 directly in its original location.
Process: “b” points to the memory location of book1, so changes to b within printBookByPointer affect book1 directly.
Memory layout (Passing by Pointer)
Struct Book | |||
book1 (204 bytes) | Pointer b (4 or 8 bytes) | ||
title (100 bytes)
|
points to book1 (4 or 8 bytes depending OS) |
Result: Total memory consumed = 208 bytes in 32-bit OS or 212 bytes in 64-bit OS.
In this example, only the address of book1 is copied (typically 4 or 8 bytes), rather than duplicating the entire struct. This saves memory and processing time, especially when the struct is large.
Advantages of using struct Pointer.
Two major advantages of using struct pointer, which are given below
1. Reduces Memory Usage: Only the memory address is copied, not the entire struct. which reduce memory consumption and Improves Performance because less time-consuming.
Note: Struct without pointer: 408 bytes memory consumption in this example. Struct with pointer: 208 bytes, saving almost half the memory in this call. |
2. Access Orginal data: Struct pointers allow programmers to modify the actual data rather than copy of struct from within functions, enabling more dynamic and complex interactions within your code.
Note: it is also proved in code example, where pages are updated from 279 to 111, but their results tells the story in program. |