SOF 103-Chapter 07 - Pointers202409
SOF 103-Chapter 07 - Pointers202409
SOF 103
C and C++ Programming
Chapter 6
Pointers
11/12/24
Pointers
2
11/12/24
Cont…
4
11/12/24
Cont…
5
11/12/24
A code snippet
6
11/12/24
Uses of Assignment Operator
7
11/12/24
Pointer Variables - Program
8
11/12/24
Accessing the Variable Pointed to
9
11/12/24
Pointers and Dynamic Variables
10
11/12/24
Cont…
11
Program Output
11/12/24
Cont…
12
Explanation
11/12/24
Basic Memory Management
13
freestore – A special area of memory reserved for
dynamic variables.
The size of freestore depends on the system and the
compiler.
If a program creates too many dynamic variables, it
may consume whole freestore area.
Additional calls to new may fail.
It is a good practice to recycle any freestore
memory that is no longer needed.
This can be accomplished by using delete operator:
delete p;
11/12/24
Defining Pointer Types
14
int *p;
11/12/24
Dynamic Arrays
15
int main()
{
typedef int *IntPtr;
IntPtr p;
int a[10];
int i;
for(i=0;i<10;i++)
a[i]=i;
p=a;
for(i=0;i<10;i++)
cout<<p[i]<<" ";
cout<<endl;
for(i=0;i<10;i++)
p[i]=p[i]+1;
for(i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
11/12/24
Explanation
17
11/12/24
Explanation
18
11/12/24
How to Use a Dynamic Array?
19
Call delete[]:
delete [] a;
11/12/24
The Relationship between Pointers and
Arrays
20
Equivalent
bptr = b;
bptr = &b[0];
11/12/24
Cont…
21
11/12/24
Cont…
22
11/12/24
Cont…
23
11/12/24
Program to demonstrate the
relationship between array and
pointer
24
11/12/24
Cont…
25
11/12/24
Output
26
11/12/24
Pointer to a string and character
array
27
11/12/24
Array of Pointers
28
11/12/24
Cont…
29
11/12/24
Output
31
11/12/24
References
32
11/12/24
Notes
33
11/12/24