English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Other

C Language Reference Manual

C Structures and Pointers

In this tutorial, you will learn to access structure members in C programming using pointers. You will also learn to dynamically allocate memory for structure types.

Before understanding how to use pointers with structures, please be sure to read the following tutorial:

C Pointer to Structure

This is the method to create a pointer to a structure.

struct name {
    member1;
    member2;
    .
    .
};
int main()
{
    struct name *ptr, Harry;
}

In this case, ptr is a pointer to a struct.

Example: Accessing members using a pointer

To access the members of a structure using a pointer, we use-> operator.

#include <stdio.h>
struct person
{
   int age;
   float weight;
};
int main()
{
    struct person *personPtr, person1;
    personPtr = &person1;   
    printf("Enter age: \t");
    scanf("%d", &personPtr)->age);
    printf("Enter weight: \t");
    scanf("%f", &personPtr)->weight);
    printf("Display:\n");
    printf("Age: \t%d\n", personPtr)->age);
    printf("Weight: %f", personPtr)->weight);
    return 0;
}

Output Result

Enter age: 25
Enter weight: 55
Display:
Age: 25
Weight: 55.000000

In this example, use personPtr = &person1; store person1whose address is stored in the personPtr pointer.

Now, you can use the personPtr pointer to access person1member.

By the way,

  • personPtr->age is equivalent to (*personPtr).age

  • personPtr->weight is equivalent to (*personPtr).weight

Dynamic Memory Allocation of Structures

Before continuing this section, it is recommended that you checkC Dynamic Memory Allocation.

Sometimes, the number of structure variables you declare may be insufficient. You may need to allocate memory at runtime. This is how you can achieve this goal in C programming.

Example: Dynamic Memory Allocation of Structures

#include <stdio.h>
#include <stdlib.h>
struct person {
   int age;
   float weight;
   char name[30];
};
int main()
{
   struct person *ptr;
   int i, n;
   printf("Enter number of people: ");
   scanf("%d", &n);
   //Allocate memory for n struct person
   ptr = (struct person*) malloc(n * sizeof(struct person));
   for(i = 0; i < n; ++i)
   {
       printf("Enter name and age: ");
         //To access the members of the first struct person,
         //You can use ptr->name and ptr->age
        //To access the members of the second struct person,
        //Using (ptr + 1)->Name and (ptr + 1)->age
       scanf("%s %d", (ptr+i)->name, &(ptr+i)->age);
   }
   printf("Display Information:\n");
   for(i = 0; i < n; ++i)
       printf("Name: %s	Age: %d\n", (ptr+i)->name, (ptr+i)->age);
   return 0;
}

When running the program, the output is:

Enter number of people:  2
Enter name and age: Harry 24
Enter name and age: Gary 32
Display Information:
Name: Harry	Age: 24
Name: Gary	Age: 32

In the above example, n struct variables were created where the user inputs n.

To allocate memory for n struct person, we use,

ptr = (struct person*) malloc(n * sizeof(struct person));

Then, we use the ptr pointer to access the elements of person.