Structures can be created and accessed using pointers. A pointer variable of a structure can be created as below:
struct name {
member1;
member2;
.....
struct name {
member1;
member2;
.....
};
int main()
{
struct name *ptr;
}
Here, the pointer variable ptr of type struct name is created.
Accessing structure's member through pointer
A structure's member can be accessed through pointer in two ways:
1.Referencing pointer to another address to access memory.
2.Using dynamic memory allocation.
1. Referencing pointer to another address to access the memory
Consider an example to access structure's member through pointer.
#include <stdio.h>
typedef struct person
{
int age;
float weight;
};
int main()
{
struct person *personPtr, person1;
personPtr = &person1;
// Referencing pointer to memory address of person1
printf("Enter Age: ");
scanf(“%d”,&personPtr->age); or scanf("%d",&(*personPtr).age);
printf("Enter Weight: ");
scanf("%f",&personPtr->weight);
printf("Displaying: ");
printf("%d%f",personPtr->age,personPtr->weight);
//can also use (*personPtr).age
return 0;
}
2. Accessing structure member through pointer using dynamic memory allocation
ptr = (cast-type*) malloc(byte-size)
Example
int main()
{
struct name *ptr;
}
Here, the pointer variable ptr of type struct name is created.
Accessing structure's member through pointer
A structure's member can be accessed through pointer in two ways:
1.Referencing pointer to another address to access memory.
2.Using dynamic memory allocation.
1. Referencing pointer to another address to access the memory
Consider an example to access structure's member through pointer.
#include <stdio.h>
typedef struct person
{
int age;
float weight;
};
int main()
{
struct person *personPtr, person1;
personPtr = &person1;
// Referencing pointer to memory address of person1
printf("Enter Age: ");
scanf(“%d”,&personPtr->age); or scanf("%d",&(*personPtr).age);
printf("Enter Weight: ");
scanf("%f",&personPtr->weight);
printf("Displaying: ");
printf("%d%f",personPtr->age,personPtr->weight);
//can also use (*personPtr).age
return 0;
}
2. Accessing structure member through pointer using dynamic memory allocation
To access structure member using pointers, memory can be allocated dynamically using malloc() function defined under "stdlib.h" header file.
Syntax to use malloc()ptr = (cast-type*) malloc(byte-size)
Example
structure's member access through pointer using malloc() function.
#include <stdio.h>
#include <stdlib.h>
struct person {
int age;
float weight;
char name[30];
};
int main()
{
struct person *ptr;
int i, num;
printf("Enter number of persons: ");
scanf("%d", &num);
ptr = (struct person*) malloc(num * sizeof(struct person));
/* Above statement allocates the memory for n structures with pointer ptr pointing to base address */
for(i = 0; i < num; ++i)
{
printf("Enter name, age and weight of the person respectively:\n");
scanf("%s%d%f", (ptr+i)->name, &(ptr+i)->age, &(ptr+i)->weight);
}
printf("Displaying Infromation:\n");
for(i = 0; i < num; ++i)
printf("%s\t%d\t%.2f\n", (ptr+i)->name, (ptr+i)->age, (ptr+i)->weight);
return 0;
}
Sample Programs
Create a structure for employee with following info: empid,name and salary. write a program to read the details of 'n' employees and display the details of employees whose salary is above 10000.Use pointer to structure. ( university question)
#include <stdlib.h>
struct person {
int age;
float weight;
char name[30];
};
int main()
{
struct person *ptr;
int i, num;
printf("Enter number of persons: ");
scanf("%d", &num);
ptr = (struct person*) malloc(num * sizeof(struct person));
/* Above statement allocates the memory for n structures with pointer ptr pointing to base address */
for(i = 0; i < num; ++i)
{
printf("Enter name, age and weight of the person respectively:\n");
scanf("%s%d%f", (ptr+i)->name, &(ptr+i)->age, &(ptr+i)->weight);
}
printf("Displaying Infromation:\n");
for(i = 0; i < num; ++i)
printf("%s\t%d\t%.2f\n", (ptr+i)->name, (ptr+i)->age, (ptr+i)->weight);
return 0;
}
Sample Programs
Create a structure for employee with following info: empid,name and salary. write a program to read the details of 'n' employees and display the details of employees whose salary is above 10000.Use pointer to structure. ( university question)
#include <stdio.h>
#include <stdlib.h>
struct Employee{
int eid;
char name[50];
float salary;
};
int main()
{
struct Employee *ptr;
int i, num;
printf("Enter number of persons: ");
scanf("%d", &num);
ptr = (struct Employee*) malloc(num * sizeof(struct Employee));
for(i = 0; i < num; ++i)
{
printf("Enter name, empid and salary:\n");
scanf("%s%d%f", (ptr+i)->name, &(ptr+i)->eid, &(ptr+i)->salary);
}
printf("Displaying Employees with salary above 10000:\n");
for(i = 0; i < num; ++i)
if((ptr+i)->salary >10000)
printf("%s\t%d\t%.2f\n", (ptr+i)->name, (ptr+i)->eid, (ptr+i)->salary);
return 0;
}
#include <stdlib.h>
struct Employee{
int eid;
char name[50];
float salary;
};
int main()
{
struct Employee *ptr;
int i, num;
printf("Enter number of persons: ");
scanf("%d", &num);
ptr = (struct Employee*) malloc(num * sizeof(struct Employee));
for(i = 0; i < num; ++i)
{
printf("Enter name, empid and salary:\n");
scanf("%s%d%f", (ptr+i)->name, &(ptr+i)->eid, &(ptr+i)->salary);
}
printf("Displaying Employees with salary above 10000:\n");
for(i = 0; i < num; ++i)
if((ptr+i)->salary >10000)
printf("%s\t%d\t%.2f\n", (ptr+i)->name, (ptr+i)->eid, (ptr+i)->salary);
return 0;
}
Programs to try
1.Create a structure for employee with following info: empid,name and salary. Write a program to read the details of 'n' employees and display the details of employees whose salary is above 10000.Use pointer to structure. ( university question)
Comments
Post a Comment