C structure using pointer
#include
#include
#include
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record1 = {1, "Raju", 90.5};
struct student *ptr;
ptr = &record1;
printf("Records of Student1: \n");
printf(" Id is: %d \n", ptr->id);
printf(" Name is: %s \n", ptr->name);
printf(" Percentage is: %f \n\n", ptr->percentage);
return 0;
}
Output
Records of Student1:
Id is: 1
Name is: Raju
Percentage is: 90.500000
Id is: 1
Name is: Raju
Percentage is: 90.500000
Copy a structure in C
#include
#include
#include
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record1 = {1, "Raju", 90.5};
struct student record2, *record3, *ptr1, record4;
printf("Records of Student1 - record1 structure \n");
printf(" Id : %d \n Name : %s\n Percentage : %f\n",
record1.id, record1.name, record1.percentage);
record2=record1;
printf("\nRecords of Student1 - Direct copy from " \ "record1 \n");
printf(" Id : %d \n Name : %s\n Percentage : %f\n",
record2.id, record2.name, record2.percentage);
ptr1 = &record1;
memcpy(record3, ptr1, sizeof(record1));
printf("\nRecords of Student1 - copied from record1 " \"using memcpy \n");
printf(" Id : %d \n Name : %s\n Percentage : %f\n",
record3->id, record3->name, record3->percentage);
printf("\nRecords of Student1 - Copied individual " \
"members from record1 \n");
record4.id=record1.id;
strcpy(record4.name, record1.name);
record4.percentage = record1.percentage;
printf(" Id : %d \n Name : %s\n Percentage : %f\n",
record4.id, record4.name, record4.percentage);
return 0;
}
Output
Records of Student1 - record1 structure
Id : 1
Name : Raju
Percentage : 90.500000
Records of Student1 – Direct copy from record1
Id : 1
Name : Raju
Percentage : 90.500000
Records of Student1 – copied from record1 using memcpy
Id : 1
Name : Raju
Percentage : 90.500000
Records of Student1 – Copied individual members from record1
Id : 1
Name : Raju
Percentage : 90.500000
Id : 1
Name : Raju
Percentage : 90.500000
Records of Student1 – Direct copy from record1
Id : 1
Name : Raju
Percentage : 90.500000
Records of Student1 – copied from record1 using memcpy
Id : 1
Name : Raju
Percentage : 90.500000
Records of Student1 – Copied individual members from record1
Id : 1
Name : Raju
Percentage : 90.500000
Structure within structure in C using normal variable
#include
#include
struct student_college_detail
{
int college_id;
char college_name[50];
};
struct student_detail
{
int id;
char name[20];
float percentage;
struct student_college_detail clg_data;
}stu_data;
int main()
{
struct student_detail stu_data = {1, "Raju", 90.5, 71145,
"Anna University"};
printf(" Id is: %d \n", stu_data.id);
printf(" Name is: %s \n", stu_data.name);
printf(" Percentage is: %f \n\n", stu_data.percentage);
printf(" College Id is: %d \n",
stu_data.clg_data.college_id);
printf(" College Name is: %s \n",
stu_data.clg_data.college_name);
return 0;
}
Output
Id is: 1
Name is: Raju
Percentage is: 90.500000
College Id is: 71145Name is: Raju
Percentage is: 90.500000
College Name is: Anna University
No comments:
Post a Comment