0% found this document useful (0 votes)
20 views27 pages

01 Lecture One

Uploaded by

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

01 Lecture One

Uploaded by

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

By

Dr. Eman Monir


Lecturer, Computer Science Department,
Faculty of Computers & Artificial Intelligence,
Benha University Lecture 1
1
Lecture One

Review On Pointers

2
Memory Allocation

0 1 2 3 4
int x;
x=24;
x
24
cout<< X; // 24 Print value
cout<< &X; //
of 6
x Print address of x

Float y;
y=81.36; 81.3 6
y
cout<< y; // 81.36 Print value of x
cout<< &y; // 26 Print address of x Memory 5
Address Operator &
• Each variable in a program is stored at a unique
location in memory that has an address.
• Use the address operator & to get the address of a
variable:
int num = -23;
cout << &num; // prints address in
hexadecimal

• The address of a memory location is a pointer


4
Pointer
s
• A variable holds a value.
• Pointer variable (pointer): a variable that holds an
address.
• Pointers provide an alternate way to access memory
locations

5
Pointer
s
• Definition:
int *p;
• Read as:
“p can hold the address of an int” or
“the variable that p points to has type int”
• The spacing in the definition does not matter:
int * p;
int*

p;
6
• * is called
Pointer Variables
• Definition and assignment:
int num = 25;
int *p;
p = &num;
• Memory num p
layout:
25 0x4a0
address of num: 0x4a00 0
• You can access num using p and indirection operator *:
cout << p; // prints 0x4a00
cout << *p; // prints 25
9
*p = 20; // puts 20 in num
Pointer Variables

• Any number of pointers can point to the same


address.
int i=5;
int *p, *q, *r;

p = &i;
q =
&i; r
= p;

8
Pointer Initialization
• You can initialize to NULL or 0 (zero)
int *ptr = 0;
• You can initialize to addresses of other variables
int num, *numPtr = &num;
int val[10], *valptr = val;
• The initial value must have the correct type
float cost;
int *ptr = &cost; // won't work

9
Pointer Variables
firstvalue is 10
#include <iostream> secondvalue is 20
using namespace std;
int main ()
{
int firstvalue, secondvalue;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << "firstvalue is "
<< firstvalue << endl;
cout << "secondvalue is "
<< secondvalue << endl;
return 0;
}
10
Pointer Variables
firstvalue is 10
#include <iostream> secondvalue is 20
using namespace std;
int main ()
{
int firstvalue = 5, secondvalue = 15;
int * p1, * p2;
p1 = &firstvalue; // p1 = address of
firstvalue p2 = &secondvalue; // p2 = address of
secondvalue
*p1 = 10; // value pointed by p1 = 10
*p2 = *p1; // value pointed by p2 = value pointed by p1
p1 = p2; // p1 = p2 (value of pointer is copied)
*p1 = 20; // value pointed by p1 = 20
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}

11
Common Bugs
• Bug #1 - Uninitialized pointers
• It occurs when you try to reference the value of
a pointer even though the pointer is
uninitialized and does not yet point to a valid
address
int *p;
*p = 12; //Uninitialized pointer

12
Common Bugs
• Bug #2 - Invalid Pointer References
• An invalid pointer reference occurs when a
pointer's value is referenced even though the
pointer doesn't point to a valid block.
int *q,*p;
p = q;
*p=25; //invalid pointer reference

13
Common Bugs
• Bug #3 - Zero Pointer Reference
• A zero pointer reference occurs whenever a pointer
pointing to zero is used in a statement that attempts
to reference a block.

• A null pointer is a regular pointer of any pointer type


which has a special value that indicates that it is not
pointing to any valid reference or memory address.
int *p;
p =
0;
*p = 12; 16
Pointers and arrays

• An array name is a pointer and it refers to the


starting address of the array.
int vals[] = {4, 7, 11}; 4 7 11
starting address of vals: 0x4a00

cout << vals; // displays 0x4a00


cout << vals[0]; // displays 4

15
Pointers and arrays

• An array name can be used as a pointer constant


int vals[] = {4, 7, 11};
cout << *vals; // displays 4
4 7 11
starting address of vals: 0x4a00

• A pointer can be used as an array name


int *valptr = vals;
cout << valptr[1]; // displays 7
16
Pointers and arrays
• Given:
int vals[]={4,7,11};
int *valptr = vals;

• What is valptr + 1? 4 7 11
s
t
a
r
t
i
n
g
17
Pointers and arrays
10, 20, 30, 40, 50,
#include <iostream>
using namespace std;
int main ()
{
int numbers[5];
int * p;
p = numbers;
*p = 10; p++;
*p = 20;
p =
&numbers[2];
*p = 30;
p = numbers +
3;
*p = 40;
p = numbers;
*(p+4) = 50;
for (int n=0;
n<5; n++)
cout << numbers[n] << ", "; 18
return 0;
Pointer Arithmetics

• Some arithmetic operators can be used with


pointers:
 Increment and decrement operators ++,
--
 Integers can be added to or subtracted from
pointers using the operators +, -, +=, and -=
 One pointer can be subtracted from another by
using the subtraction operator - 19
Pointer Arithmetics

• Assume the variable definitions


int vals[]={4,7,11};
int *valptr = vals; 4 7 11
s
t
a
r
t
i
n
g
20
Pointer Arithmetics

• Assume the variable definitions


int vals[]={4,7,11};
int *valptr = vals; 4 7 11
s
t
a
r
t
i
n
g
21
Pointer Arithmetics

• Assume the variable definitions


int vals[]={4,7,11};
int *valptr = vals; 4 7 11
s
t
a
r
t
i
n
g
22
Pointer Arithmetics
• Assume the variable definitions
int vals[]={4,7,11};
int *valptr = vals;
4 7 11
• Example of pointer subtraction starting address of vals: 0x4a00
valptr += 2;
cout << valptr - vals;
// This statement prints 2: the number of
// ints between valptr and vals

23
Pointer Arithmetics
• Suppose that we define three pointers:
char *mychar;
short *myshort;
long *mylong;

• Assume we know
that they point to
memory locations
1000, 2000 and
3000 respectively
• If we write:
mychar++; 24
Pointer Arithmetics

mychar = mychar + 1;
myshort = myshort + 1;
mylong = mylong + 1;

25
Pointer Arithmetics
• The following expression may lead to confusion:
*p++;

• Because ++ has greater precedence than *, this expression


is equivalent to *(p++).
• Notice the difference with:

(*p)++

26
27

You might also like