C Structure Pointer

Ads
 

C Structure Pointer

This section illustrates you the concept of Structure Pointer in C.

This section illustrates you the concept of Structure Pointer in C.

C Structure Pointer

     

This section illustrates you the concept of Structure Pointer in C. You can see in the given example, we want to access the employee's information through structure pointer. For that, we declare the structure type st consisting of members id, name and address. Then we have declared a structure pointer as:

struct st employee, *stptr;

We point the pointer to employee with the expression stptr = &employee. Then access the given members by de-referencing the pointer. For this, we have used another structure selection operator which works on pointers to structures. If p is a pointer to a structure and m is a member of that structure, then p->m. We set the values of specified members as:

stptr->id = 1;
stptr->name = "Angelina";
stptr->address ="Rohini,Delhi";

Here is the code:

STPOINTE.C

#include <stdio.h>
#include <conio.h>

int main() {
  struct st {
  int id;
  char *name;
  char *address;
  };
  struct st employee, *stptr;
  stptr = &employee;
  stptr->id = 1;
  stptr->name = "Angelina";
  stptr->address ="Rohini,Delhi";
  printf("Employee Information: id=%d\n%s\n%s\n", stptr->id, stptr->name,
  stptr->address);
  getch();
  return 0;
}

Output will be displayed as:

STPOINTE.EXE

Download Source Code:

Ads

Ads