Professional Documents
Culture Documents
Pointersarevariables,whichcontaintheaddressof
someothervariables.
Declaration:
odata type*pointer name;
oe.g.int*ptra;
Thetypeofapointerdependsonthetypeofthevaria
bleitpointsto.Every pointer points to some data
type.
All data is stored in memory. But different data
types occupydifferent amount of memory.
Q14.Pointers to structure?
struct student {
char name[50];
char major [20];
double gpa;
} STUDENT;
STUDENT bob = {"Bob Smith", "Math", 3.77};
STUDENT sally = {"Sally", "CSEE", 4.0};
STUDENT *pStudent; /* pStudent is a
"pointer to struct student" */
/* make pStudent point to bob */
pStudent = &bob;
/* use -> to access the members */
printf ("Bob's name: %s\n", pStudent->name);
printf ("Bob's gpa : %f\n", pStudent->gpa);
/* make pStudent point to sally */
pStudent = &sally;
printf ("Sally's name: %s\n", pStudent->name);
printf ("Sally's gpa: %f\n", pStudent->gpa);
Pointer to struct for functions:
void PrintStudent(STUDENT *studentp)
{ printf(“Name : %s\n”, studentp->name);
printf(“Major: %s\n”, studentp->major);
printf(“GPA : %4.2f”, studentp->gpa); }
Q14. Advantage of using pointer?
1. Pointers are more efficient in handling
arrays and data tables.
2. Pointers can be used to return multiple
values from a function.
3. Pointers allow c to support dynamic
memory management.
4. Pointer reduces length and complexity of
program.
5. Pointer increase execution speed and
thus reduces the program execution time.
Q15. What is Chain of pointer?
Void main ()
{
int x, *p1, **p2;
X=100;
P1=&x;
P2=&p1;
Printf(“%d”, **p2);
}
The pointer variable p2 contains the address of
the pointer variable p1, which points to the
location that contains the desired value.
This is known as multiple indirections.