Structure pointer in C
×


Structure pointer in C

31

In C programming, a structure pointer refers to a pointer variable that holds the memory address of a structure.

It provides a way to access and manipulate the members of a structure using pointers, offering more flexibility and efficiency in handling complex data structures.

Syntax Structure pointer in C

struct structureName {
    dataType member1;
    dataType member2;
    // ... more members
};
struct structureName *pointerName;

Definition:

A user-defined data type in C that groups variables of different data types under a single name.

Pointer to Structure:

A pointer variable that stores the memory location of a structure.

Declaration:

struct Student {
    char name[50];
    int age;
};
struct Student *ptrStudent;

Initialization:

struct Student std = {"John", 25};
ptrStudent = &std;

Examples:

Accessing Structure Members using Pointer:

// program for Structure Pointer in C
#include<stdio.h>

struct Student {
    char name[50];
    int age;
};

int main() {
    struct Student std = {"John", 25};
    struct Student *ptrStudent;

    ptrStudent = &std;

    printf("Student Name: %s\n", ptrStudent->name);
    printf("Student Age: %d\n", ptrStudent->age);

    return 0;
}

Output:

Student Name: John
Student Age: 25

Dynamic Allocation of Structure using Pointer:

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>   // Include this header for strcpy()

struct Student {
    char name[50];
    int age;
};

int main() {
    struct Student *ptrStudent;

    ptrStudent = (struct Student *) malloc(sizeof(struct Student));

    if(ptrStudent == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    ptrStudent->age = 30;
    strcpy(ptrStudent->name, "Alice");  // Use strcpy() to copy the string

    printf("Student Name: %s\n", ptrStudent->name);
    printf("Student Age: %d\n", ptrStudent->age);

    free(ptrStudent);

    return 0;
}
		
	

Output:

Student Name: Alice
Student Age: 30

Function Returning Structure Pointer:

#include<stdio.h> 
#include<stdlib.h> 
struct Point {
    int x;
    int y;
};

struct Point* createPoint(int x, int y) {
    struct Point *ptr = (struct Point*) malloc(sizeof(struct Point));
    
    if(ptr == NULL) {
        return NULL;
    }

    ptr->x = x;
    ptr->y = y;
    
    return ptr;
}

int main() {
    struct Point *point = createPoint(5, 10);

    if(point != NULL) {
        printf("Point coordinates: (%d, %d)\n", point->x, point->y);
        free(point);
    } else {
        printf("Memory allocation failed\n");
    }

    return 0;
}
	

Output:

Point coordinates: (5, 10)

The -> operator is used to access the members of a structure through a pointer.

Dynamic memory allocation using malloc() is common for pointer-to-structure, and it's essential to free the allocated memory using free() to avoid memory leaks.

Structure pointers offer efficient access and manipulation of structure members, especially when dealing with arrays of structures or linked lists.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments