Structs and Pointers in C

In this lesson, you will learn how to access a struct by using a pointer variable, along with examples, to understand the topic better.

Accessing structs using pointers in C

In the lesson Struct Basics in C, we learned how to access a struct by using a normal struct variable. However, a struct can be accessed by a pointer, where a pointer variable points to the struct variable’s address.

Example

struct student {
  char name[20];
  int age;
  char grade[1];
};

struct student student;

// declare a pointer to the struct of type struct student
struct student * studentPointer;

In the example above, we declared a pointer studentPointer that stores the address of the struct student.

Accessing elements using pointers

Now let’s access the struct student members’ by using a pointer. In C, there are multiple ways to access a struct’s element using a pointer: The Asterisk * and the dot . operators, as well arrow -> operator.

1- Using the asterisk (*) and Dot (.) Operator

The asterisk * and Dot  . operators allow us to access a member of a struct in C.

(*studentPointer).name // Points to the name
(*studentPointer).age // Points to the age
Note: the parentheses around the pointer are necessary as the dot . operator is greater than the asterisks *

Example

#include <stdio.h>

#include <string.h>

struct Student {
  char name[20];
  int age;
  char grade[1];
};

int main(int argc, char * argv[]) {
  struct Student student1, student2;

  strcpy(student1.name, "Fred");
  strcpy(student1.grade, "A");
  student1.age = 21;

  strcpy(student2.name, "Steve");
  strcpy(student2.grade, "B");
  student2.age = 23;

  struct Student * studentPointer; // define struct pointer

  studentPointer = & student1; // points studentPointer to student1

  printf("Values in Pointer &student1 \n");

  printf(( * studentPointer).name);
  printf(" \n");
  printf(( * studentPointer).grade);
  printf(" \n");

  printf(" \n");

  studentPointer = & student2; // points studentPointer to student2
  printf("Values in Pointer &student2 \n");
  printf(( * studentPointer).name);
  printf(" \n");
  printf(( * studentPointer).grade);
  printf(" \n");

  printf(" \n");
}

Output

Values in Pinter &student1

Fred
A

Values in Pointer &student2

Steve

B

2- Using the arrow operator (->)

Another method, which is preferred due to its clearness and simplicity, is the arrow -> operator’s use.

studentPointer->name // Points to the name 
studentPointer->age // Points to the age

In this method, the asterisks and dot operators are omitted, making the code clear and easier to understand.

Example

#include <stdio.h>

#include <string.h>

struct Student {
  char name[20];
  int age;
  char grade[1];
};

int main(int argc, char * argv[]) {
  struct Student student1, student2;

  strcpy(student1.name, "Fred");
  strcpy(student1.grade, "A");
  student1.age = 21;

  strcpy(student2.name, "Steve");
  strcpy(student2.grade, "B");
  student2.age = 23;

  struct Student * studentPointer; // define struct pointer

  studentPointer = & student1; // points studentPointer to student1

  printf("Values in Pinter &student1 \n");

  printf(studentPointer -> name);
  printf(" \n");
  printf(studentPointer -> grade);
  printf(" \n");

  printf(" \n");

  studentPointer = & student2; // points studentPointer to student2
  printf("Values in Pointer &student2 \n");
  printf(studentPointer -> name);
  printf(" \n");
  printf(studentPointer -> grade);
  printf(" \n");

  printf(" \n");
}

Output

Values in Pinter &student1

Fred

A

Values in Pointer &student2

Steve

B

Points to remember

  • In addition to a normal struct variable, a struct in C can be accessed by pointers as well.
  • In C, The Asterisk * and Dot . can access structs.
  • The arrow -> operator is a cleaner way to access a struct in C.
Back to: Learn C Programming > Structures and Unions

Leave a Reply

Your email address will not be published. Required fields are marked *