C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Pointers in C

Pointer to structure

C Program: Pointer to structure

C

#include <stdio.h>

 

// Define a structure

struct Student {

    int roll;

    char name[50];

    float marks;

};

 

int main() {

    struct Student s1 = {101, "Ravi Kumar", 89.5};

    struct Student *ptr;  // Pointer to structure

 

    // Assign address of structure to pointer

    ptr = &s1;

 

    // Access structure members using pointer and arrow operator

    printf("Student Details:\n");

    printf("Roll Number: %d\n", ptr->roll);

    printf("Name: %s\n", ptr->name);

    printf("Marks: %.2f\n", ptr->marks);

 

    return 0;

}

Output

 
OUTPUT :
Student Details:
Roll Number: 101
Name: Ravi Kumar
Marks: 89.50

Explanation

  • struct Student defines three members: roll, name, and marks.
  • A pointer ptr of type struct Student* is declared.
  • The address of structure variable s1 is assigned to ptr.
  • The arrow operator (->) is used to access structure members through a pointer.