Download Document
Showing pages : 1 - 3 of 11
This preview has blurred sections. Sign up to view the full version! View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Copyright @ 2009 Ananda Gunawardena Lecture 09 C Structs and Linked Lists In this lecture Structs in C b operator Structs within BMP files Reading Header information from BMP files Passing structs to functions Passing pointer to structs Array of structs and Array of struct pointers Concept of a linked list Types of Linked List Implementation Further readings Exercises Answers Structs in C Structs in C are used to package several data fields into one unit. Structs can be used to define types and can be used to define variables of that type. C structs are different from Java classes. Java Classes encapsulates both state (data fields) and behavior (methods) with these fields being public, private or protected. C structs can only declare public data fields and all fields are accessible using the dot (.) operator. For example, if we define a typedef for a struct that contain two fields, var1 and var2 as: typedef struct { unsigned int var1; char* var2; } node; and a variable mynode of type node node mynode; fields within mynode variable can be accessed using, mynode.var1 = 100; mynode.var2 = malloc(20); =================== (1) Copyright @ 2009 Ananda Gunawardena The amount of memory required to hold the variable mynode is equivalent to sizeof(unsigned int) + sizeof(char*) . You can also write sizeof(node) to find the how many bytes are required to hold a variable of size node. Note that sizes of specific data types are dependent on the machine you may be using. For example, on andrew domain machines(unix.andrew.cmu.edu), an address variable is 4 bytes while in CS domain machines (linux.gp.cs.cmu.edu) an address variable is 8 bytes. As with initializing variables as they are declared, structs can also be initialized as node mynode = {100, malloc(20)}; b operator There are two ways to access fields within a struct. Field within a struct can be accessed using the dot operator. However, if a pointer to the struct is given, then we can access fields within the struct using -> operator. For example, node mynode; node* ptr = &mynode; ptr b var1 = 100; ptr b var2 = malloc(20); is equivalent to the code given above in (1) using the dot operator. The arrow operator will be used extensively in cases where a pointer to the struct is passed to a function instead of a copy. Structs within BMP files An interesting example of a struct type is header information stored in a bitmap (BMP) file. BMP, a format invented by Microsoft stores the image using a schema as follows. The first 14 bytes is reserved for information given by the following struct typedef struct { unsigned short int type; /* BMP type identifier */ unsigned int size; /* size of the file in bytes*/ unsigned short int reserved1, reserved2; unsigned int offset; /* starting address of the byte */ } HEADER; The next 40 bytes are reserved for a structure as follows. ...
View Full Document