0% found this document useful (0 votes)
44 views23 pages

Structures, Unions in C

Structures in C allow programmers to create custom data types that group together different data types under a single name. Structures contain member variables of basic data types. Members are accessed using the dot operator. Structures can be passed by value to functions like basic data types, but pointers to structures provide a more efficient way to manipulate structure members by reference. Unions allow sharing of the same memory location between different types of variables.

Uploaded by

shyamoha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views23 pages

Structures, Unions in C

Structures in C allow programmers to create custom data types that group together different data types under a single name. Structures contain member variables of basic data types. Members are accessed using the dot operator. Structures can be passed by value to functions like basic data types, but pointers to structures provide a more efficient way to manipulate structure members by reference. Unions allow sharing of the same memory location between different types of variables.

Uploaded by

shyamoha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

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

Variables in a structure are called members


2

Memory View of Structures


struct Point pt;
pt Name

Memory allocated for pt = SUM of memories allocated for its member variables

Basic of Structures (contd)


Access a member of structure (. notation to access members)
structure-name.member

members can be explicitly assigned values


pt.Name=firstPoint; pt.x=10; pt.y=20; printf(x = %d, y = %d\n, pt.x, pt.y);
4

Basic of Structures (contd)


Structures can be initialized during declaration struct Point pt=,newPt, 10, 20-;

By default, they are initialized to 0 (or `\0')


Size of a structure is the combined size of its members
5

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

Structures and Functions


When structures are passed into functions all of their values are copied. (pass by value, like basic data types. Arrays are different) A function must return the structure to affect the target structure. This is a lot of copying of variable values onto and off the stack. (inefficient) Pointers will be used to make this efficient.
7

Functions returning structures

Passing structures to functions

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

To access its element:


l1.pt1.x=10; The

operator has left-to-right associativity


13

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;

Memory occupied: the dimensions of the array multiply by sizeof(struct tag)


(Remember) sizeof() is compile time function
14

Pointers in Structures
A structure can have a pointer as its member
struct Point { char *namePtr; int x; int y; };

struct Point pt;


pt namePtr

pt.namePtr=(char *) malloc(20*sizeof(char)); *(pt.namePtr)=lastPoint; pt


namePtr
x 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

Self referencing Structures


Useful in data structures like trees, linked lists. It is illegal for a structure to contain an instance of itself. Solution: Have a pointer to another instance.
struct lnode { /* the linked list node */ int value; struct lnode *nextPtr; /* pointer to next node */ } n1,n2;

n1 value nextPtr
0 NULL

n2 value nextPtr
0 NULL

17

Self referencing Structures


n1.value=10; n1.nextPtr=&n2; n2.value=20; n2.nextPtr=NULL; struct lnode *basePtr=&n1;

basePtr

0x1000

n1 value 10 0x1000 nextPtr 0x2000

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;

In context of structs, you can do:


struct Point { int x; int y; }; typedef struct Point myPoint; myPoint p1; struct Point p2; p1.x=10; typedef struct Point *pointPtr; typedef struct lnode { pointPtr p1; . struct Point p2; . p2.x=20; } myNode; p1.x=10; ?? myNode n1, *ptr; p1x=10; ?? typedef struct { p1=&p2; . p1x=10; ?? p1=(pointPtr) malloc(sizeof(struct Point)); . } myNode; p1x=10; ?? myNode n1, *ptr;
19

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;

Each of ival, fval, cval have the same location in memory.


sizeof(union ) = maximum of sizeof(field)

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

You might also like