Structure Pointer

Pointer Variable can also Store the Address of the Structure Variable. Pointer to Array of Structure stores the Base address of the Structure array.

2.1 Write a program to store 1 student information using a structure pointer concept without using array.

MEMORY MAPPING:
1)initially it allocates size of the structure.
2)firstly it allocates 10 memory locations for my name=”priyanka”
3)next it allocates 35 memory locations for mailid,for both age and roll_num it allocates each of 4 byte
for below program i got addressess will be as follow(note address will be changing at each compilation and depending on different PC’s.

/*name=priyanka->0xbf946424
mailid=priyankamn522@gmail.com->0xbf94642e
roll_num=65->0xbf946454
age=22->0xbf946458*/

#include<stdio.h>
#include<string.h>//adding inbuilt for string
library(particularly strcpy)
struct student//structure definition along with tag name
{
char name[10],mailid[35];
int roll_num,age;
};
int main()
{
struct student a,*ptr;//declaration of structure pointer variable
ptr=&a;//pointer variable stores the address of structur variable.
strcpy(ptr->name,”priyanka”);
strcpy(ptr->mailid,”priyankamn522@gmail.com”);
ptr->roll_num=65;
ptr->age=22;
printf(“name=%s->%p\n mailid=%s->%p\n roll_num=%d->%p\n age=
%d->%p\n”,ptr->name,ptr->name,ptr->mailid,ptr->mailid,ptr-
>roll_num,&ptr->roll_num,ptr->age,&ptr->age);
/*printing the one student information using pointers*/
}

2.2 write a program using structure pointer to store 5 students
information without using array.

MEMORY MAPPING:

1st student
name->0xbfd4d7d8
age->0xbfd4d808
rollno->0xbfd4d80c
mailid ->0xbfd4d7e2
marks->0xbfd4d810

2nd student
name ->0xbfd4d814
age ->0xbfd4d844
rollno ->0xbfd4d848
mailid ->0xbfd4d81e
marks ->0xbfd4d84c

3rd student
name ->0xbfd4d850
age ->0xbfd4d880
rollno ->0xbfd4d884
mailid ->0xbfd4d85a
marks ->0xbfd4d888

4th student
name ->0xbfd4d88c
age ->0xbfd4d8bc
rollno ->0xbfd4d8c0
mailid ->0xbfd4d896
marks ->0xbfd4d8c4

5th student
name ->0xbfd4d8c8
age ->0xbfd4d8f8
rollno ->0xbfd4d8fc
mailid ->0xbfd4d8d2
marks ->0xbfd4d900 */

#include<stdio.h>
struct student//structure defination along with data type
declaration of the members
char name[10],mailid[35];
int age,rollno,marks;
int main()
struct student a[10],*ptr;//declaration of structure array along with structure pointer int i;
for(i=0;i<5;i++)//for loop for scannning students information by using pointer concept
{
ptr=&a[i];
printf(“enter the %d student
information:name,age,rollno,mailid,marks\n”,i+1); scanf(“%s %d %d %s %d”,ptr->name,&ptr->age,&ptr >rollno,ptr->mailid,&ptr->marks); ptr++;//for next student information storage location
}
for(i=0;i<5;i++)//for loop for printing the 5 student information using structue pointers and structure array concept
{
ptr=&a[i];
printf(“student %d information is\n name=%s-%p age=%d-
%p rollno=%d-%p mailid=%s-%p marks=%d-%p\n”,i+1,ptr->name,ptr-
>name,ptr->age,&ptr->age,ptr->rollno,&ptr->rollno,ptr-
>mailid,ptr->mailid,ptr->marks,&ptr->marks);
ptr++;//next student information address
}
}

2.3 write a program using structure pointer to store 5 students
information without using array.

MEMORY MAP:

according to my user input
name =priya=0xbf9f84d4
rollno =65=0xbf9f84e0
age =22=0xbf9f84e4
marks =79=0xbf9f84e8
———————————-
name =rita=0xbf9f84ec
rollno =87=0xbf9f84f8
age =23=0xbf9f84fc
marks =67=0xbf9f8500
———————————-
name =sony=0xbf9f8504
rollno =21=0xbf9f8510
age =21=0xbf9f8514
marks =78=0xbf9f8518
———————————-
name =sheela =0xbf9f851c
rollno =71 =0xbf9f8528
age =21 =0xbf9f852c
marks =48 =0xbf9f8530
———————————-
name =smitha =0xbf9f8534
rollno =81 =0xbf9f8540
age =21 =0xbf9f8544
marks =78 =0xbf9f8548
———————————-*/

#include<stdio.h>
struct student//structure defination along with the datatype
declaration of the members
{
char name[10];
int rollno,age,marks;
};
int main()
{
struct student a,b,c,d,e,*p1,*p2,*p3,*p4,*p5;//structure variable declaration along with the structure pointer declaration
p1=&a,p2=&b,p3=&c,p4=&d,p5=&e; printf(“enter the student 1 information name,rollno,age,marks\n”);

//user input of 5 students information one by one using pointer
scanf(“%s %d %d %d”,p1->name,&p1->rollno,&p1->age,&p1- >marks);

printf(“enter the student 2 information name,rollno,age,marks\n”); scanf(“%s %d %d %d”,p2->name,&p2->rollno,&p2->age,&p2- >marks);

printf(“enter the student 3 information name,rollno,age,marks\n”); scanf(“%s %d %d %d”,p3->name,&p3->rollno,&p3->age,&p3- >marks);

printf(“enter the student 4 information name,rollno,age,marks\n”); scanf(“%s %d %d %d”,p4->name,&p4->rollno,&p4->age,&p4- >marks);

printf(“enter the student 5 information name,rollno,age,marks\n”); scanf(“%s %d %d %d”,p5->name,&p5 >rollno,&p5->age,&p5- >marks); 

//printing the five students information using structure pointers:

printf(“the first student information is\n name=%s-%p rollno=%d-%p age=%d-%p marks=%d-%p\n”,p1->name,p1->name,p1- >rollno,&p1->rollno,p1->age,&p1->age,p1->marks,&p1->marks);

printf(“the second student information is\n name=%s-%p rollno=%d-%p age=%d-%p marks=%d-%p\n”,p2->name,p2->name,p2- >rollno,&p2->rollno,p2->age,&p2->age,p2->marks,&p2->marks);

printf(“the third student information is\n name=%s-%prollno=%d-%p age=%d-%p marks=%d-%p\n”,p3->name,p3->name,p3->rollno,&p3->rollno,p3->age,&p3->age,p3->marks,&p3->marks);

printf(“the fourth student information is\n name=%s-%p rollno=%d-%p age=%d-%p marks=%d-%p\n”,p4->name,p4->name,p4- >rollno,&p4->rollno,p4->age,&p4->age,p4->marks,&p4->marks);

printf(“the fifth student information is\n name=%s-%p rollno=%d-%p age=%d-%p marks=%d-%p\n”,p5->name,p5->name,p5- >rollno,&p5->rollno,p5->age,&p5->age,p5->marks,&p5->marks);
}