C-Programming

Structures and pointer

We know that the name of an array stands for the address of its zeroth element. The same thing is true of the names of arrays of structure variables. We can declare the structure as a pointer variable as in simple variable.

Syntax:

struct tag

{

…………

………….

};

struct tag *ptr;

For example:

struct student

{

char name[20];

int roll;

float average;

};

struct student *std;

Here the statement struct student *std; defines a pointer to an student. The member variables of the pointer structure variable can be accessed by the following notation:

std->name

std->roll

std->average

The symbol -> is called the arrow operator and is made up of a minus sign and a greater than sign. Since pointer acts as an array therefore we can treat the pointer as a one dimensional array and can access the member variable as in the array. It should be noted that there is no need of declaring another array if you have declared structure as pointer which can hold the infinite number of structure.

Structures and functions (Prev Lesson)
(Next Lesson) How to use pointer as an array in the structure?
Back to C-Programming

No Comments

Post a Reply

Teacher
Bhim Gautam
Role : Lecturer
Read More
error: Content is protected !!