Home C-tutorials C Structure Pointer
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

C Structure Pointer

Advertisement
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:

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 6, 2009

Ask Questions?    Discuss: C Structure Pointer   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
pekon
August 29, 2012
thanks.. for C tutorial

Hi, not many people share their knowledge or help others for free. thanks for for ur help in posting such tutorial. keep up this effort.. with regards, pekon
DMCA.com