Sie sind auf Seite 1von 29

IIT Bombay

Computer Programming
Dr. Deepak B Phatak
Dr. Supratik Chakraborty
Department of Computer Science and Engineering
IIT Bombay
Session: Structures and Pointers Part 1

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Relevant Topics


IIT Bombay

Structures as collections of variables/arrays/other


structures
Statically declared variables/arrays of structure types
Accessing members of structures
Organization of main memory: locations and addresses
Pointers to variables/arrays of basic data types

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overview of This Lecture


IIT Bombay

Pointers to variables of structure types


Accessing members of structures through pointers

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Recall: Memory and Addresses

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

MAIN

Address (in hexadecimal)


400
10011101
Main memory is a sequence of
401
10111111
physical storage locations
402
10010001
Each location stores 1 byte (8 bits) 403 1 0 1 1 0 1 1 1
404
10010001
Content/value of location
405
10000111
406
11110001
Each physical memory location
407
10000000
identified by a unique address
408
11111111
Index in sequence of memory
00000000
409
locations
11110000
40a

MEMORY

IIT Bombay

Memory for Executing a Program (Process)

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

STACK SEGMENT

DATA

SEGMENT
CODE SEGMENT

MAIN

Operating system allocates a


part of main memory for use
by a process
Divided into:
Code segment: Stores
executable instructions in
program
Data segment: For
dynamically allocated data
Stack segment: Call stack

MEMORY

IIT Bombay

Structures in 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

Needs 4 bytes of storage

Structures in Main Memory

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

Structures in 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

Needs 1 + 4 + 4,
i.e. 9 bytes of
storage

Structures in Main Memory

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

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

Structures in Main Memory

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

p1.z
p1.x
p1.y

STACK SEGMENT

a
DATA SEGMENT
CODE SEGMENT

MAIN

int main()What is that


{
gap/padding?
struct MyStructType {
char z;Wait for a few
int x, y; slides
};
MyStructType p1;
int a;
Rest of code
return 0;
}

MEMORY

IIT Bombay

10

Structures in Main Memory

record of main in call stack

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

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

What Can We Safely Assume About Structures?

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

What Can We Safely Assume About Structures?


p1.z
p1.x
p1.y

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

What Can We Safely Assume About Structures?


p1.z

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

What Can We Safely Assume About Structures?


p1.z
p1.x
p1.y

STACK SEGMENT

Memory locations allocated for each


member are
DATA SEGMENT
however contiguous (have consecutive addresses).
E.g., four contiguous locationsCODE
for p1.x,
SEGMENT
four contiguous locations for p1.y
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

MAIN

struct MyStructType {
char z;
int x, y;
};
MyStructType p1;

MEMORY

IIT Bombay

15

Recall: & and * Operators


IIT Bombay

We used & and * operators with variables of basic data


types
Pointer-to-integer
int a;
data type
int * ptrA;
ptrA = &a;
*ptrA = 10;

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

16

Recall: & and * Operators


IIT Bombay

We used & and * operators with variables of basic data


types
Address of (starting location) of
int a;
variable a of type int
int * ptrA;
ptrA = &a;
*ptrA = 10;

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

17

Recall: & and * Operators


IIT Bombay

We used & and * operators with variables of basic data


types
int a;
Contents (as int) of memory locations
whose starting address is given by ptrA
int * ptrA;
ptrA = &a;
*ptrA = 10;

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

18

& and * Operators for Structures


IIT Bombay

We can use & and * operators with variables of


structure types in exactly the same way
int a;
struct MyStructType {
char z; int x, y;
int * ptrA;
};
ptrA = &a;
MyStructType
p1;
*ptrA = 10;
MyStructType * ptrP1;
ptrP1 = &p1;
*ptrP1 = {c, 2, 3};
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

19

& and * Operators for Structures


IIT Bombay

We can use & and * operators with variables of


structure types in exactly the same way
Pointer-to-MyStructType
int
a;
struct MyStructType {
data type
char z; int x, y;
int * ptrA;
};
ptrA = &a;
MyStructType
p1;
*ptrA = 10;
MyStructType * ptrP1;
ptrP1 = &p1;
*ptrP1 = {c, 2, 3};
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

20

& and * Operators for Structures


IIT Bombay

We can use & and * operators with variables of


structure types in exactly the same way
Address of (starting
int
a;
location)
of variable p1 of struct MyStructType {
char z; int x, y;
int *type
ptrA;
MyStructType
};
ptrA = &a;
MyStructType
p1;
*ptrA = 10;
MyStructType * ptrP1;
ptrP1 = &p1;
*ptrP1 = {c, 2, 3};
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

21

& and * Operators for Structures


IIT Bombay

We can use & and * operators with variables of


structure types in exactly the same way
Contents (as MyStructType)
a;
struct MyStructType {
ofint
memory
locations whose
char z; int x, y;
int * ptrA;
starting
address is given by
};
ptrP1
ptrA = &a;
MyStructType
p1;
*ptrA = 10;
MyStructType * ptrP1;
ptrP1 = &p1;
*ptrP1 = {c, 2, 3};
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

22

Accessing Members Through Pointers


IIT Bombay

Can we access p1.x through


ptrP1?
Yes, and by the obvious way:
E.g. (*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 = {c, 2, 3};

23

Accessing Members Through Pointers


IIT Bombay

Can we access p1.x through


ptrP1?
Yes, and by the obvious way:
E.g. (*ptrP1).x = 1 + (*ptrP1).y;

*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

Accessing Members Through Pointers


IIT Bombay

Can we access p1.x through


ptrP1?
Yes, and by the obvious way:
E.g. (*ptrP1).x = 1 + (*ptrP1).y;

struct MyStructType {
char z; int x, y;
};
MyStructType p1;
MyStructType * ptrP1;
(*ptrP1).x is the member x of ptrP1 = &p1;
*ptrP1 = {c, 2, 3};

the object (*ptrP1) of


type MyStructType

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

25

Accessing Members Through Pointers


IIT Bombay

Can we access p1.x through


ptrP1?
Yes, and by the obvious way:
E.g. (*ptrP1).x = 1 + (*ptrP1).y;

C++ provides the -> operator for


above situations
E.g. ptrP1->x = 1 + ptrP1->y;

struct MyStructType {
char z; int x, y;
};
MyStructType p1;
MyStructType * ptrP1;
ptrP1 = &p1;
*ptrP1 = {c, 2, 3};

ptrVar->memberName is equivalent to (*ptrVar).memberName


Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

26

Accessing Members Through Pointers


IIT Bombay

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

Accessing Members Through Pointers


IIT Bombay

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

Pointers to variables of structure data types


Use of & and * operators with structures
Use of -> operator to access members of structures
through pointers.

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

29

Das könnte Ihnen auch gefallen