Structures, Unions in C
Structures, Unions in C
Basic of Structures
Definition: A collection of one or more different variables with the same handle (same name) Structures are customized (programmer-defined) data types Keyword struct is used to declare structures and create variables of customized data types
struct Point pt;
Declar ation of new data type Point struct Point { char name[30]; int x; int y; }; struct Point { . . } pt1, pt2; Variables pt, pt1, pt2 are of type Point
Memory allocated for pt = SUM of memories allocated for its member variables
struct Apt { int Number; int MBedroomSize; // in square feet int BedroomSize; int BalconySize; float BathSize; double MBathSize; }; // No variables created // } name1, name2, name3; // Now variables are made. struct Apt MyPlace = {311, 400, 200, 100, 125.50, 175.50}; // New global variable int main() { printf(Apt No: %d\n", MyPlace.Number); printf("MyBedroom: %d\n", MyPlace.MBedroomSize); printf("Bedroom: %d\n", MyPlace.BedroomSize); printf(Balcony: %d\n", MyPlace.BalconySize); printf("Bath: %f\n", MyPlace.BathSize); printf("My Bath: %lf\n", MyPlace.MBathSize); return 0; }
6
Pointers to Structures
Pointers are an easier way to manipulate structure members by reference The entire structure is not passed by value, only the address of the first member Use arrow operator () for accessing the struct element
struct Date MyDate, *DatePtr; DatePtr = &MyDate; DatePtr->month = 2; DatePtr->day = 22;
10
Pointers to structures
11
Example
12
Nested Structures
Structs can also contain other structs as l1 members pt1 pt2
struct Line { struct Point pt1; struct Point pt2; }; struct Line l1;
Name
Name
Array of Structures
Array of Structures act like any other array.
struct Point pt[3]; pt*0+.name = A; pt[0].x = 0; pt[0].y = 1; pt*1+.name = B; pt[1].x = 4; pt[1].y = 1; pt*2+.name = mid; pt[2].x = (pt[0].x + pt[1].x)/2; pt[2].y = (pt[0].y + pt[1].y)/2;
Pointers in Structures
A structure can have a pointer as its member
struct Point { char *namePtr; int x; int y; };
lastPoint \0
15
Pointer to Structures
A pointer to a structure can be defined struct Point p1, *ptr; ptr=&p1;
p1.x=10 ptrx =10 (*ptr).x=10 (&p1)x = 10
16
n1 value nextPtr
0 NULL
n2 value nextPtr
0 NULL
17
basePtr
0x1000
n2 value 20 0x2000
18
nextPtr NULL
Typedef
Use typedef for creating new data type names typedef int length; this the name length a synonym (alias) for int. Afterwards, you can do: length x = 4;
Unions
A union is a memory location that is shared by two or more different types of variables.
union u_tag { int ival; float fval; char cval; } u;
Usage is similar to that of structs: u.ival or u.cval Up to programmer to determine how to interpret a union (i.e. which member to access) and used for low-level programming
20
Example
union AnElt { int i; char c; } elt1, elt2; elt1.i = 4; elt2.c = a; elt2.i = 0xDEADBEEF;
padding
EF
BE
i
AD
DE
21
Unions
Storage
size of union is the size of its largest member avoid unions with widely varying member sizes; for the larger data types, consider using pointers instead
Initialization
Union may only be initialized to a value appropriate for the type of its first member
22
Bit-fields
When storage is high cost affair, we need to use memory efficiently (e.g in embedded systems)
struct { unsigned pin1 : 1; unsigned pin2 : 2; unsigned pin3 : 1; } flags;
Here each of the element takes a bit of memory (1 bit) The number following the colons represent the field length in bits.
23