0% found this document useful (0 votes)
4 views29 pages

CS101x S438A Structures and Pointers Part 1 IIT Bombay

The document provides an overview of structures and pointers in programming, specifically focusing on memory organization, accessing structure members through pointers, and the use of '&' and '*' operators. It discusses the layout of structures in memory, the allocation of memory for processes, and how to manipulate structure data using pointers. The content is aimed at enhancing understanding of how structures work in programming languages, particularly in the context of C/C++.

Uploaded by

abhishekhamida
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)
4 views29 pages

CS101x S438A Structures and Pointers Part 1 IIT Bombay

The document provides an overview of structures and pointers in programming, specifically focusing on memory organization, accessing structure members through pointers, and the use of '&' and '*' operators. It discusses the layout of structures in memory, the allocation of memory for processes, and how to manipulate structure data using pointers. The content is aimed at enhancing understanding of how structures work in programming languages, particularly in the context of C/C++.

Uploaded by

abhishekhamida
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/ 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 1


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

2 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

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


Recall: Memory and Addresses
IIT Bombay

Address (in hexadecimal)


• 400 10011101

MEMORY
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
• Each physical memory location 406 11110001
407 10000000
identified by a unique address 408 11111111

MAIN
• Index in sequence of memory 409 00000000
locations 40a 11110000

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


Memory for Executing a Program (Process)
IIT Bombay

• Operating system allocates a


part of main memory for use

MEMORY
by a process STACK SEGMENT
• Divided into:
Code segment: Stores DATA SEGMENT
executable instructions in
program
CODE SEGMENT

MAIN
Data segment: For
dynamically allocated data
Stack segment: Call stack
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 5
Structures in Main Memory
IIT Bombay

int main()
{
struct MyStructType {
char z;
int x, y;
}; Needs 4 bytes of storage
MyStructType p1;
int a;
… Rest of code …
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 6
Structures in Main Memory
IIT Bombay

int main()

MEMORY
{ STACK SEGMENT
struct MyStructType {
char z;
int x, y;
};
MyStructType p1; a
int a; DATA SEGMENT
… Rest of code …

MAIN
return 0; CODE SEGMENT
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 7
Structures in Main Memory
IIT Bombay

int main()
{ Needs 1 + 4 + 4,
struct MyStructType { i.e. 9 bytes of
char z; storage
int x, y;
};
MyStructType p1;
int a;
… Rest of code …
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 8
Structures in Main Memory
IIT Bombay

int main()

MEMORY
{ STACK SEGMENT
p1.z
struct MyStructType { p1.x
char z;
int x, y; p1.y
};
MyStructType p1; a
int a; DATA SEGMENT
… Rest of code …

MAIN
return 0; CODE SEGMENT
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 9
Structures in Main Memory
IIT Bombay

int main()What is that

MEMORY
{ STACK SEGMENT
gap/padding? p1.z
struct MyStructType { p1.x
char z;Wait for a few
int x, y; slides p1.y
};
MyStructType p1; a
int a; DATA SEGMENT
… Rest of code …

MAIN
return 0; CODE SEGMENT
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 10
Structures in Main Memory
IIT Bombay

int main()

MEMORY
{ STACK SEGMENT
p1.z
struct MyStructType { p1.x
char z;
int x, y; p1.y
};
MyStructType p1; a
int a; DATA SEGMENT
… Rest of code
p1,…a: local variables of “main”

MAIN
return
Memory0; for “p1” and “a” allocatedCODE
in activation
SEGMENT
}
record of “main” in call stack
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 11
What Can We Safely Assume About Structures?
IIT Bombay

struct MyStructType {

MEMORY
STACK SEGMENT
char z; p1.z
int x, y; p1.x
}; p1.y
MyStructType p1;
No assumptions about
DATA SEGMENT
relative layout of different
members within memory

MAIN
CODE SEGMENT
allocated for a structure
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 12
What Can We Safely Assume About Structures?
IIT Bombay

struct MyStructType {

MEMORY
STACK SEGMENT
char z; p1.z
int x, y; p1.x
}; p1.y
MyStructType p1;

DATA SEGMENT
No assumptions about
“padding” (unused memory locations) after locations

MAIN
CODE SEGMENT
allocated for different members of a structure
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 13
What Can We Safely Assume About Structures?
IIT Bombay

struct MyStructType {

MEMORY
STACK SEGMENT
char z; p1.z
int x, y; p1.x
}; p1.y
MyStructType p1;

DATA SEGMENT
No assumptions about
“padding” (unused memory locations) after locations

MAIN
CODE SEGMENT
allocated for different members of a structure
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 14
What Can We Safely Assume About Structures?
IIT Bombay

struct MyStructType {

MEMORY
STACK SEGMENT
char z; p1.z
int x, y; p1.x
}; p1.y
MyStructType p1;
Memory locations allocated for each member are
DATA SEGMENT
however contiguous (have consecutive addresses).

MAIN
E.g., four contiguous locationsCODE
for p1.x,
SEGMENT
four contiguous locations for p1.y
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 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
int * ptrA; whose starting address is given by “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 {
int * ptrA; char z; int x, y;
ptrA = &a; };
*ptrA = 10; MyStructType p1;
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
int * ptrA; char z; int x, y;
ptrA = &a; };
*ptrA = 10; MyStructType p1;
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 {
int *type
ptrA;
MyStructType char z; int x, y;
ptrA = &a; };
*ptrA = 10; MyStructType p1;
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”)
ofint a;
memory locations whose struct MyStructType {
int * ptrA;
starting address is given by char z; int x, y;
“ptrP1”
ptrA = &a; };
*ptrA = 10; MyStructType p1;
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 struct MyStructType {


ptrP1? char z; int x, y;
• Yes, and by the obvious way: };
E.g. (*ptrP1).x = 1 + (*ptrP1).y; MyStructType p1;
MyStructType * ptrP1;
ptrP1 = &p1;
*ptrP1 = {‘c’, 2, 3};

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


Accessing Members Through Pointers
IIT Bombay

• Can we access p1.x through struct MyStructType {


ptrP1? char z; int x, y;
• Yes, and by the obvious way: };
E.g. (*ptrP1).x = 1 + (*ptrP1).y; MyStructType p1;
MyStructType * ptrP1;
ptrP1 = &p1;
*ptrP1 is an object of *ptrP1 = {‘c’, 2, 3};
type MyStructType

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


Accessing Members Through Pointers
IIT Bombay

• Can we access p1.x through struct MyStructType {


ptrP1? char z; int x, y;
• Yes, and by the obvious way: };
E.g. (*ptrP1).x = 1 + (*ptrP1).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 struct MyStructType {


ptrP1? char z; int x, y;
• Yes, and by the obvious way: };
E.g. (*ptrP1).x = 1 + (*ptrP1).y; MyStructType p1;
MyStructType * ptrP1;
ptrP1 = &p1;
C++ provides the “->” operator for
above situations *ptrP1 = {‘c’, 2, 3};
E.g. ptrP1->x = 1 + ptrP1->y;
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 { struct MyStructType {


char z; int x, y; char z; int x, y;
}; };
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 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

You might also like