Share on Google+Share on Google+

Pointer and Structure in C

Advertisement
Structures in C defines the group of contiguous (adjacent) fields, such as records or control blocks. A structure is a collection of variables grouped together under a single name. It provides an elegant and powerful way for keeping related data together.

Description:

In this tutorial you will see how pointer is used with structure. This is a very important example because all data-structure program work on this principle.

Code:

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

struct record
{
char name[20];
int roll;
};
struct record r,*p;

void main()
{
p=&r;
clrscr();
printf("\nEnter the name\n");
scanf("\n%s",&p->name);
printf("\nEnter the roll no\n");
scanf("%d",&p->roll);
printf("\n%s",p->name);
printf("\n%d",p->roll);
}

Code Description:

p=&r assign the address of object to p
*p allow the pointer to access the objects
*p.roll is same as p->roll like used in example.

Output:

Download this code

Advertisements

Advertisement

Posted on: April 29, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.