Passing large structures by value to C functions copies all internal bytes onto the call stack, causing performance overhead. Passing structure pointers (struct Node *) passes a single 8-byte memory address, accessing internal fields efficiently using the arrow operator (->).
Structure Pointer & Arrow Operator Example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct DeviceNode {
int device_id;
char name[32];
};
// Function accepting structure pointer (Modifies original struct in place)
void update_device(struct DeviceNode *dev, int new_id, const char *new_name) {
dev->device_id = new_id;
snprintf(dev->name, sizeof(dev->name), "%s", new_name);
}
int main(void) {
// Dynamic heap allocation for structure
struct DeviceNode *node = malloc(sizeof(struct DeviceNode));
if (node != NULL) {
update_device(node, 42, "Ethernet Controller");
printf("Device ID: %d, Name: %s\n", node->device_id, node->name);
free(node);
}
return 0;
}
Comments and corrections