CS101x S438A Structures and Pointers Part 1 IIT Bombay PDF
CS101x S438A Structures and Pointers Part 1 IIT Bombay PDF
Computer Programming
Dr. Deepak B Phatak
Dr. Supratik Chakraborty
Department of Computer Science and Engineering
IIT Bombay
Session: Structures and Pointers Part 1
MAIN
MEMORY
IIT Bombay
STACK SEGMENT
DATA
SEGMENT
CODE SEGMENT
MAIN
MEMORY
IIT Bombay
int main()
{
struct MyStructType {
char z;
int x, y;
};
MyStructType p1;
int a;
Rest of code
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
STACK SEGMENT
a
DATA SEGMENT
CODE SEGMENT
MAIN
int main()
{
struct MyStructType {
char z;
int x, y;
};
MyStructType p1;
int a;
Rest of code
return 0;
}
MEMORY
IIT Bombay
int main()
{
struct MyStructType {
char z;
int x, y;
};
MyStructType p1;
int a;
Rest of code
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
Needs 1 + 4 + 4,
i.e. 9 bytes of
storage
p1.z
p1.x
p1.y
STACK SEGMENT
a
DATA SEGMENT
CODE SEGMENT
MAIN
int main()
{
struct MyStructType {
char z;
int x, y;
};
MyStructType p1;
int a;
Rest of code
return 0;
}
MEMORY
IIT Bombay
p1.z
p1.x
p1.y
STACK SEGMENT
a
DATA SEGMENT
CODE SEGMENT
MAIN
MEMORY
IIT Bombay
10
MAIN
int main()
{
STACK SEGMENT
p1.z
struct MyStructType {
p1.x
char z;
p1.y
int x, y;
};
MyStructType p1;
a
int a;
DATA SEGMENT
Rest of code
p1,a: local variables of main
return
0;
SEGMENT
Memory
for p1 and a allocatedCODE
in activation
}
MEMORY
IIT Bombay
11
No assumptions about
relative layout of different
members within memory
allocated for a structure
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
p1.z
p1.x
p1.y
STACK SEGMENT
DATA SEGMENT
CODE SEGMENT
MAIN
struct MyStructType {
char z;
int x, y;
};
MyStructType p1;
MEMORY
IIT Bombay
12
STACK SEGMENT
DATA SEGMENT
No assumptions about
padding (unused memory locations) after locations
CODE SEGMENT
allocated for different members of
a structure
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
MAIN
struct MyStructType {
char z;
int x, y;
};
MyStructType p1;
MEMORY
IIT Bombay
13
STACK SEGMENT
p1.x
p1.y
DATA SEGMENT
No assumptions about
padding (unused memory locations) after locations
CODE SEGMENT
allocated for different members of
a structure
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
MAIN
struct MyStructType {
char z;
int x, y;
};
MyStructType p1;
MEMORY
IIT Bombay
14
STACK SEGMENT
MAIN
struct MyStructType {
char z;
int x, y;
};
MyStructType p1;
MEMORY
IIT Bombay
15
16
17
18
19
20
21
22
struct MyStructType {
char z; int x, y;
};
MyStructType p1;
MyStructType * ptrP1;
ptrP1 = &p1;
*ptrP1 = {c, 2, 3};
23
*ptrP1 is an object of
type MyStructType
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
struct MyStructType {
char z; int x, y;
};
MyStructType p1;
MyStructType * ptrP1;
ptrP1 = &p1;
*ptrP1 = {c, 2, 3};
24
struct MyStructType {
char z; int x, y;
};
MyStructType p1;
MyStructType * ptrP1;
(*ptrP1).x is the member x of ptrP1 = &p1;
*ptrP1 = {c, 2, 3};
25
struct MyStructType {
char z; int x, y;
};
MyStructType p1;
MyStructType * ptrP1;
ptrP1 = &p1;
*ptrP1 = {c, 2, 3};
26
struct MyStructType {
char z; int x, y;
};
MyStructType p1;
MyStructType * ptrP1;
ptrP1 = &p1;
*ptrP1 = {c, 2, 3};
(*ptrP1).x = 1 + (*ptrP1).y;
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
struct MyStructType {
char z; int x, y;
};
MyStructType p1;
MyStructType * ptrP1;
ptrP1 = &p1;
ptrP1->z = c; ptrP1->x = 2;
ptrP1->y = 3;
ptrP1->x = 1 + ptrP1->y;
27
struct MyStructType {
struct MyStructType {
char z; int x, y;
char z; int x, y;
};
Functionally equivalent}; program fragments
MyStructType p1;
MyStructType p1;
MyStructType * ptrP1;
MyStructType * ptrP1;
ptrP1 = &p1;
ptrP1 = &p1;
*ptrP1 = {c, 2, 3};
ptrP1->z = c; ptrP1->x = 2;
(*ptrP1).x = 1 + (*ptrP1).y;
ptrP1->y = 3;
ptrP1->x = 1 + ptrP1->y;
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
28
Summary
IIT Bombay
29