0% found this document useful (0 votes)
0 views25 pages

Structures in C Language

Lecture 9 covers the concepts of structure types, including their definition, syntax for declaration, and memory allocation. It discusses the use of structures with pointers, functions, and arrays, as well as nested structures and bitwise structures. The lecture also highlights the differences between structures and arrays, and how structures can be used to manage complex data types effectively.

Uploaded by

Muhammad Haroon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views25 pages

Structures in C Language

Lecture 9 covers the concepts of structure types, including their definition, syntax for declaration, and memory allocation. It discusses the use of structures with pointers, functions, and arrays, as well as nested structures and bitwise structures. The lecture also highlights the differences between structures and arrays, and how structures can be used to manage complex data types effectively.

Uploaded by

Muhammad Haroon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Lecture 9 Structure types

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

• Syntax to access structure members


Use dot operator (or structure member operator) . to represent each member
as a variable, and be used to set/get values or get address.
struct_var. v1, &struct_var.v1
struct_var.v2, & struct_var.v2

• Syntax to initialize a structure variable


struct struct_name struct_var = {constant1, constant2, …};
–Initializers should match their corresponding member types in the structure
definition. The rest will be set as 0.
Example score
(4 bytes)
struct student {
int id; name
char name[20]; (20 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;

• Define new type name


struct student { typedef struct student { typedef struct {
int id; int id; int id;
char name[20]; char name[20]; char name[20];
float score; float score; float score;
}; } STUDENT; } STUDENT;
typedef struct student STUDENT; STUDENT s1;
struct student s1; STUDENT s1;
STUDENT s2; struct student s2;

sizeof(STUDENT) gives 28
1234 s1.id
STUDENT s1; John
s1.id = 1234; 88.0

strcpy(s1.name, “John”); // can not use s1.name = “John”;


s1.score = 88.0

STUDENT s1 = {1234, “John“, 88.0}; // declare and initialization

s1.id holds value 1234

s1.name stores string “John”

s1.score holds value 88.0

STUDENT s2 = {5678, “William“};

s2.id holds value 5678

s2.name stores string “William”

s2.score holds value 0


Example
struct point {
int x;
int y;
} p1; // define struct point and declare variable p1
struct point p2; // declare p2

typedef struct {
int x;
int y;
} POINT;

POINT p1, p2;

What is the sizeof(POINT)?


Declare and initialize
POINT p1 = {320, 100};
POINT p2 = {200, 100};
printf("%d %d", p1.x, p1.y)
Declare, set values, get values
p1.x = 320; printf("%d %d", p2.x, p2.y)
p1.y = 100;
printf(“%d,%d”,p1.x, p1.y);

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;

STUDENT s3 = s1; // declare and initialize by value of s1 b = a;

or

STUDENT s3; // declare

s3 = s1; // assign value

s3.id has value 1234

s3.name stores string “John”

s3.score has value 88.0


Nested structures
• A type of a structure member can be any already defined type.
• A previously defined structure type can be used to define members in a new
structure type, called a nested structure.
• Nested structures enable the construction of hierarchical data types.
Example
typedef struct typedef struct typedef struct
{ char first_name[20]; { int dd; { int id;
char mid_name[20]; int mm; NAME name;
char last_name[20]; int yy; DATE dob;
} NAME; } DATE; } PERSON;

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;

• The address of p1 is &p1,


• The address of p1.x is &p1.x

POINT *ptr; // declare POINT type pointer


ptr = &p1; // assign the address of p1 to ptr

Representation equivalence:

ptr -> x  (*ptr).x  p1.x


&(ptr->x)  &ptr->x  &p1.x  &(*ptr).x
Self-referential structures
• A self-referential structure is a structure type that contains a
pointer variable of its own type.
• Self-referential structure is the foundation of many other data
structures such as linked lists, trees.
Example
struct node { // this defines type name: struct node
int val;
struct node *next; // this define a pointer of struct node type
};
struct node has two member variables int val and struct node *next next
is a pointer pointing to a struct node type variable.
Example:
typedef struct node {
int val;
struct node *next;
} NODE;

NODE n1, n2, *p1 = &n1;


n1.val = 1;
n1.next = &n2;
n2.val = 2;
n2.next = NULL;

printf(“%d”, p1->val); // output: 1


printf(“%d”, p1->next->val); // output: 2
Access n2 from n1, this is the foundation of linked list.
3. Structures with functions
• Passing a structure type value to a function is the same as passing
a basic type value to a function. That is, the value of the structure
type variable will be copied into the function like local variables.
• Syntax for passing a structure variable to a function:
data_type func_name(struct struct_name argument);
Example
void display_student(STUDENT s) {
printf(“id:%d;name:%s;score:%.1f”, s.id, s.name,s.score);
}
STUDENT s1={1234,”Johe”,88.0};
display_student(s1); // output: id:1234;name:John;score:88.0
// whole s1 (28 bytes) is copied into function.

void display(POINT p) { printf(“%d, %d”, p.x, p.y); }


POINT p1={2,3};
display(p1); // output 2,3, whole p1 (8 bytes) is copied into the function
• Passing structure variable reference to a function is the same
passing reference of a basic type variable to a function.

• 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 = &p[0]; /* assign the address of p or p[0] to pt */

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy