Skip to content

CSPC2005/01. Construct and Execute a C Program Showing the Use of Pointer to Structure

 Updated: 2 hours read

To understand and demonstrate the use of pointers to structures in C programming by constructing and executing a simple C program. This lab will help students grasp how pointers can be used to reference and manipulate data within structures.

Table of contents

Open Table of contents

Instructions

Repo

  1. Go to s-m-quadri/geca-labs repository.
  2. Switch branch to CSPC2005/01 corresponding to current lab.
  3. Click on CODE, then Codespaces, then Create, to setup in-browser IDE.
  4. Wait until VS Code gets open.
  5. Write program from scratch, compile, execute. Then only tally with already available solution.

    IMP: Make sure to delete after completion of lab. Otherwise your monthly free-hours limit can exhaust. It can be achieved by:

    1. Closing in-browser VS Code tab.
    2. Click on CODE, then Codespaces, then Delete, to delete virtual space.

1. Create a Structure

  1. Define a structure to represent a Student with fields such as name, age, and grade.
#include <stdio.h>

// Define structure
struct Student {
    char name[50];
    int age;
    float grade;
};

2. Declare a Pointer to the Structure

  1. Declare a structure variable and a pointer to that structure.
int main() {
    // Declare structure variable
    struct Student student1;

    // Declare a pointer to structure
    struct Student *ptr;
}

3. Assign Pointer to Structure Variable

  1. Initialize the pointer by assigning it the address of the structure variable student1.
    // Assign the address of student1 to the pointer
    ptr = &student1;

4. Access Structure Members Using the Pointer

  1. Use the pointer to access and modify structure members.
    // Accessing members using the pointer
    printf("Enter student's name: ");
    scanf("%s", ptr->name);

    printf("Enter student's age: ");
    scanf("%d", &ptr->age);

    printf("Enter student's grade: ");
    scanf("%f", &ptr->grade);

5. Display the Structure Data via Pointer

  1. Use the pointer to display the contents of the structure.
    // Display data using pointer
    printf("\nStudent Information:\n");
    printf("Name: %s\n", ptr->name);
    printf("Age: %d\n", ptr->age);
    printf("Grade: %.2f\n", ptr->grade);

    return 0;
}

6. Compile and Execute

  1. Compile the program using a C compiler. If you are using GCC, the command will be:
    gcc pointer_to_structure.c
    
  2. Run the executable:
    ./a.out
    

Expected Output

Enter student's name: John
Enter student's age: 21
Enter student's grade: 85.5

Student Information:
Name: John
Age: 21
Grade: 85.50

Explanation

Spoiler

(Full source code) Do not copy-paste
#include <stdio.h>

// Define structure
struct Student {
    char name[50];
    int age;
    float grade;
};

int main() {
    // Declare structure variable
    struct Student student1;

    // Declare a pointer to structure
    struct Student *ptr;

    // Assign the address of student1 to the pointer
    ptr = &student1;

    // Accessing members using the pointer
    printf("Enter student's name: ");
    scanf("%s", ptr->name);

    printf("Enter student's age: ");
    scanf("%d", &ptr->age);

    printf("Enter student's grade: ");
    scanf("%f", &ptr->grade);

    // Display data using pointer
    printf("\nStudent Information:\n");
    printf("Name: %s\n", ptr->name);
    printf("Age: %d\n", ptr->age);
    printf("Grade: %.2f\n", ptr->grade);

    return 0;
}

Conclusion

In this lab, we successfully implemented a C program to demonstrate the use of pointers with structures. You learned how to declare a pointer to a structure, access structure members using the pointer, and manipulate structure data dynamically.

Key Takeaways

Exercises

  1. Modify the program to store details for multiple students using an array of structures.
  2. Implement a function that takes a pointer to a structure as an argument and modifies the structure’s values.

This concludes the lab on using pointers to structures in C.

References

  1. C Pointers (With Examples). Programiz.
  2. C Programming: Structure and Pointer
  3. Structures, Pointers - Learn C - Free Interactive C Tutorial. www.learn-c.org.

Previous Post
CSPC2006/02. Creating Wireframes and Prototypes
Next Post
CSPC2006/01. Creating User Research and User Profiles