Structures in C Language
Structures in C Language
Student
1. Concepts of structure types
2. Structures and pointers
3. Structures with functions
4. Structures and arrays
5. Bitwise structures
1. Concepts of structure types
1. A structure type (or simple structure) is a user-defined
or application specific data type, consisting of a list of
data members (elements), each with its own data
types. A structure type data is also called a record in
applications.
2. The size of a structure type is the sum of sizes of all its
members. A structure type value is stored in contiguous
memory cells with its members one after another.
Operations can be applied to each member.
3. A structure type is a composite data type, combining
different data types. A structure type is used to
construct structured hierarchical data types.
• Syntax to define a structure type:
struct struct_name {
data_type1 v1;
data_type2 v2;
......
data_typek vk;
};
– The data_type1, data_type2, …, data_typek, can be any data
types which have been defined.
– sizeof(struct struct_name) is equal to
sizeof(data_type1) + sizeof(data_type2)+…+
sizeof(data_typek)
– The structure definition does not allocate any memory. It
defines a template that tells the compiler the types and
ordering of structure members of the structure type.
• Syntax to declare structure type variable:
struct struct_name struct_var;
This tells compiler to allocate a memory block of sizeof(struct struct_name)
bytes
float score; Id
(4 bytes)
};
– This defines a structure type named struct student
struct student s1;
– This declares a variable s1 of the struct student type.
– sizeof(struct student) or sizeof(s1) is equal to
sizeof(int) + sizeof(name) + sizeof(float) = 28 bytes
– Compiler allocate a memory block of 28 bytes to s1, id has the lowest address,
followed by name and score in the memory block.
– Use s1.id, s1.name, s1.score to access members id, name and score. i.e.
s1.id = 1234; strcpy(s1.name, “john”); s1.score = 88.0;
Keyword typedef is used to define a type name
• Rename an existing type
typedef int INT; // this define a new type name INT for the int type.
INT a = 10; // this is the same as int a = 10;
sizeof(STUDENT) gives 28
1234 s1.id
STUDENT s1; John
s1.id = 1234; 88.0
typedef struct {
int x;
int y;
} POINT;
p2.x = 300;
p2.y = 300;
printf(“%d,%d”,p2.x, p2.y);
Structure types support assignment operations
• structure type variable can be assigned to another structure type
variable of the same type, can be used for initialization.
Example
int a = 5;
STUDENT s1 = {1234, “John“, 88.0}; int b;
or
PERSON p1;
strcpy(p1.name.first_name , “John”);
p1.dob.dd = 15;
p1.dob.mm = 3;
p1.dob.yy= 1990;
2. Structures and pointers
• A structure type pointer can be declared to hold the address of a
structure type variable.
• Use structure pointer operator “->” to access the member of the
structure.
• Syntax:
struct struct_name {
data_type1 v1;
data_type2 v2;
};
struct struct_name variable_name;
struct struct_name *ptr = &variable_name;
ptr->v1 is equivalent to variable_name. v1
typedef struct {
int x; int* a;
int y;
POINT* p1;
} POINT p1;
POINT p1;
Representation equivalence:
• Syntax:
data_type func_name(struct struct_name *reference);
Example
void inc(POINT *p) {
p->x = p->x + 1;
p->y = p->x + 1;
}
POINT p1={2,3};
inc(&p1); // the address of p1 is copied to call stack
display(p1); // output: 3, 4
Structure type as function return type
• When a structure type is used as a return type of a function, the
structure variable value is copied out from the function local
scope, similar to that of returning a basic type variable.
Syntax:
struct struct_name function(parameter list) {
struct struct_name local_variable = {0};
......
return local_variable;
}
struct struct_name external_variable = function(argument list);
Example
POINT point_inc(POINT p) {
p.x = p.x+1;
p.y = p.y+1;
return p;
}
POINT p1={2,3};
POINT p2 = point_inc(p1);
display(p2); //display 3,4
4. Structure arrays
• A structure array (array of of structures) is an array, in which each
element is of the same structure type.
– The representations of structure arrays are the same as basic type arrays.
– Arrays of structures are commonly in used to represent / store / process data
records in application programs.
• Syntax of declaring a structure array:
struct struct_name name[length];
Example
STUDENT studuents[30];
students[0].id = 9;
strcpy(&students[0].name, “Peter”);
Example
POINT p[10];
/* declare an array of point types */
POINT *pt;
/* define a pointer variable for structure point */
pt + 1 is pointing to p[1]
(pt+1) -> x is equivalent to p[1].x
Structure array to functions
• Passing a structure array to a function is the same as
passing a basic type array to a function.
Examples:
void display_all(POINT p[], int n){
int i;
for (i=0; i<n; i++) printf("%d, %d\n", p[i].x, p[i].y);
}
void display_all2(POINT *p, int n){
int i;
for (i=0; i<n; i++){printf("%d, %d\n", p->x, p->y);p++;}
}
POINT pa[2]={2,3,4,5}; // define array of 2 POINT type
display_all(pa, 2); // pass array name as reference
display_all2(pa, 2); // pass array name as pointer
POINT *p=pa;
display_all2(p, 2); // pass POINT pointer
Structures vs Arrays
1. Both structures and arrays store data elements in contiguous memory
locations.
2. All array elements are of the same data type, elements are represented by
subscript index.
Structure’s elements may have different data type, elements are represented
by dot operator.
3. Structures support assignment, arrays do not.
4. Structure support return type, arrays do not.
5. Passing structure variable to a function copies the structure data to the
function local. Passing array name to a function copies the address of array to
function local.
Note: If you really want to copy array data into a function through arguments, you
can define a structure type wrapping the array, then pass the structure type value
to the function.
5. Bitwise structures
• A bit field is a structure member used to store multiple logical
values as a short series of bits where each of the single bits can
be addressed separately. A bitwise structure is a structure
containing bit fields.
Example
typedef struct packet {
unsigned a:2; // has has 2 bits
unsigned b:6; // has has 6 bits
unsigned c:4; // has has 4 bits
unsigned d:4; // has has 4 bits
int i:16; // has 16 bits
} PACKET;
PACKET data;
data.a = 3; data.b = 7; data.c = 9; data.d = 15; data.i = 256;
printf(“%d, %d, %d, %d”, data.a, data.b, data.c, data.d);
printf(“%u, %o, %x, %d”, data.a, data.b, data.c, data.d);
a b c d i
11 0 00111 1001 1111 00000000100000000
• Application of bit fields
– A bit field is used to represent the sign of int type
– A usage of bit-fields is to represent single bit flags with each
flag stored in a separate bit.
– Packet header. E.g., IP4 header