Académique Documents
Professionnel Documents
Culture Documents
Structure Assignment
You can use structures just like variables:
StudentRecord s1,s2; s1.name = "Joe Student"; Copies the entire structure s2 = s1;
Array of Structure
struct student { int roll; char name[20]; int age; Char class[8]; } s[50]; Example: S[1].roll=100; S[5].roll=1005; S[1].age=30;
Union: A union is like a struct, but only one of its members is stored, not all Union sample { int x; float y; Char z; }; union sample code;
1200
1201
1202
1203
All the members of the structure can be accessed at once,where as in an union only one member can be used at a time. Another important difference is in the size allocated to a structure and an union. For eg: struct example { int integer; float floating_numbers; } the size allocated here is sizeof(int)+sizeof(float); where as in an union union example { int integer; float floating_numbers; } size allocated is the size of the highest member. so size is=sizeof(float); All the members of the structure can be accessed at once,where as in an union only one member can be used at a time.
sizeof ( type-name )
The operand can also be a data type, in which case the result is the length in bytes of objects of that type:
sizeof(char) /* 1
sizeof(float) /* 4
sizeof(int ) /* 2
Example
You can use the sizeof operator to obtain information about the sizes of objects in your C environment. The following prints the sizes of the basic data types: /* Program name is "sizeof_example". This program * demonstrates a few uses of the sizeof operator. */ #include <stdio.h> int main(void) { printf("TYPE\t\tSIZE\n\n"); printf("char\t\t%d\n", sizeof(char)); printf("short\t\t%d\n", sizeof(short)); printf("int\t\t%d\n", sizeof(int)); printf("float\t\t%d\n", sizeof(float)); printf("double\t\t%d\n", sizeof(double)); }