Structure within structure in C using pointer 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, *stu_data_ptr;
int main()
{
struct student_detail stu_data = {1, "Raju", 90.5, 71145,
"Anna University"};
stu_data_ptr = &stu_data;
printf(" Id is: %d \n", stu_data_ptr->id);
printf(" Name is: %s \n", stu_data_ptr->name);
printf(" Percentage is: %f \n\n",
stu_data_ptr->percentage);
printf(" College Id is: %d \n",
stu_data_ptr->clg_data.college_id);
printf(" College Name is: %s \n",
stu_data_ptr->clg_data.college_name);
return 0;
}
Output
Id is: 1
Name is: Raju
Percentage is: 90.500000
Name is: Raju
Percentage is: 90.500000
College
Id is: 71145
College Name is: Anna University
College Name is: Anna University
Memory allocation in C structure
#include
#include
#include
struct student
{
int id1;
int id2;
char a;
char b;
float percentage;
};
int main()
{
int i;
clrscr();
struct student record1 = {1, 2, 'A', 'B', 90.5};
printf("size of structure in bytes : %d\n",
sizeof(record1));
printf("\nAddress of id1 = %u", &record1.id1 );
printf("\nAddress of id2 = %u", &record1.id2 );
printf("\nAddress of a = %u", &record1.a );
printf("\nAddress of b = %u", &record1.b );
printf("\nAddress of percentage = %u",&record1.percentage);
return 0;
}
Output
size of structure in bytes : 16
Address of id1 = 675376768
Address of id2 = 675376772
Address of a = 675376776
Address of b = 675376777
Address of percentage = 675376780
Address of id1 = 675376768
Address of id2 = 675376772
Address of a = 675376776
Address of b = 675376777
Address of percentage = 675376780
Structure padding in C
#include
#include
#include
struct structure1
{
int id1;
int id2;
char name;
char c;
float percentage;
};
struct structure2
{
int id1;
char name;
int id2;
char c;
float percentage;
};
int main()
{
struct structure1 a;
struct structure2 b;
printf("size of structure1 in bytes : %d\n", sizeof(a));
printf ( "\n Address of id1 = %u", &a.id1 );
printf ( "\n Address of id2 = %u", &a.id2 );
printf ( "\n Address of name = %u", &a.name );
printf ( "\n Address of c = %u", &a.c );
printf ( "\n Address of percentage = %u",&a.percentage );
printf(" \n\nsize of structure2 in bytes : %d\n",sizeof(b));
printf ( "\n Address of id1 = %u", &b.id1 );
printf ( "\n Address of name = %u", &b.name );
printf ( "\n Address of id2 = %u", &b.id2 );
printf ( "\n Address of c = %u", &b.c );
printf ( "\n Address of percentage = %u",&b.percentage );
getchar();
return 0;
}
Output
size of structure1 in bytes : 16
Address of id1 = 1297339856
Address of id2 = 1297339860
Address of name = 1297339864
Address of c = 1297339865
Address of percentage = 1297339868
size of structure2 in bytes: 20
Address of id1 = 1297339824
Address of name = 1297339828
Address of id2 = 1297339832
Address of c = 1297339836
Address of percentage = 1297339840
Address of id1 = 1297339856
Address of id2 = 1297339860
Address of name = 1297339864
Address of c = 1297339865
Address of percentage = 1297339868
size of structure2 in bytes: 20
Address of id1 = 1297339824
Address of name = 1297339828
Address of id2 = 1297339832
Address of c = 1297339836
Address of percentage = 1297339840
No comments:
Post a Comment