0% found this document useful (0 votes)
13 views6 pages

Review 01

The document explains the concepts of references and pointers in programming, highlighting their definitions, usage, and examples. It discusses static pointers, dynamic variables, pointer arithmetic, and the importance of managing memory with the delete keyword. Additionally, it emphasizes that arrays and strings can be treated as pointers and provides examples of their implementation.

Uploaded by

alqisimohammad46
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)
13 views6 pages

Review 01

The document explains the concepts of references and pointers in programming, highlighting their definitions, usage, and examples. It discusses static pointers, dynamic variables, pointer arithmetic, and the importance of managing memory with the delete keyword. Additionally, it emphasizes that arrays and strings can be treated as pointers and provides examples of their implementation.

Uploaded by

alqisimohammad46
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/ 6

Reference

A reference is another name for given to a variable of


the same data type

int x=3; 6
int &y=x;
cout<<x+y;

Example
defining variable x. and assigning to it different names
m,n,z

int x=4;
int &z=x;
16
int&m=x,&n=x;
cout<<x+m+n+z;

Static Pointers
A pointer is a variable that contains the address of
another variable of the same data type

int x=4;
cout<<"adrs of x="<<&x<<endl; adrs of x=0x7ffe805ec44c
int *p=&x; adrs of x is p=0x7ffe805ec44c
cout<<"adrs of x is p="<<p;

We can print the value of the variable by using the


*pointer

int g=8; *q=8


int *q=&g; g=8
cout<<"*q="<<*q<<endl;
cout<<"g="<<g<<endl;
1
Page

MEng. Samah A. Massadeh


Notice that *q is a reference to g - another name given
to the same memory location both *q and g are aliases to
x(alias means another name)

Example of a static pointer and a reference

double x=5; 15
double &z=x;
double *p=&x;
if(x==*p && z==*p)
cout<<x+*p+z;

Static pointer arithmetic

int x=1;
int *p=&x;
p++;
*p=2; 321
p++;
*p=3;
cout<<*p;
p--;
cout<<*p;
p--;
cout<<*p;

______________________________________________________________________________
_

Example trace this code


int x=0;
int *g=&x;
for(int i=0;i<4;i++){
*g=i; cout<<*g; 0123
++g; 3210
2

}
Page

cout<<"\n \n"; for(int j=3;j>=0;j--){


MEng. Samah A. Massadeh
g--;
cout<<*g;
}

NULL pointer
We must assign a pointer to NULL to prohibit programs
crashing
Dangling pointer not assigned to NULL (shorted pointers)
should be shorted or removed

Example
4
int *q=NULL;//initialize the pointer to null
int x=4;
q=&x;
cout<<*q; //output 4
q=NULL;

Dynamic variables (pointers)


Are pointers (related to run time and stored in the
heap)
Type *name= new Type(value);

Keyword new returns an address on the heap

Example

int x(3); 39
cout<<x;
int *y=new int (9);
cout<<*y;

Precedence in incrementing the value instead of the


pointer use brackets

Example
3
Page

int m=int(7);
MEng. Samah A. Massadeh
int *q=&m; 85
++(*q) ; 55
56
cout<<*q;
int *y= new int(5);
cout<<*y<<endl;
*y=55;
cout<<*y<<endl;
++(*y);
cout<<*y;
Keyword: delete frees reserved memory locations
Dynamic pointers cause depletion of memory so we have
to remove them from the heap, by using delete keyword
to free the memory
When we use delete, the memory location will be freed
for writing new values but the old value remains until
we write over it

Example 9
1.14241e-313
double *z=new double(9.0);
cout<<*z<<endl;
delete z;
cout<<*z;

Pointers are arrays – strings


Example
assign string c to pointer p
onet
char c[]="onet\0wo";
char *p=c;
int i=0;
while (p[i] != '\0')
{
cout<<*p;
p++;
}

Example
4

assign array of integer a. to pointer q


Page

MEng. Samah A. Massadeh


int a[3]={0,1,2};
int *q=a; 012
for (int i=0;i<3;i++)
cout<<q[i];

5
Page

MEng. Samah A. Massadeh


Summery
Pointers are faster in execution time
 Static pointers should be shorted (assigned to
NULL), and cannot be changed at run time dynamic
pointers should be deleted (free memory locations)
 Arrays and strings are pointers

6
Page

MEng. Samah A. Massadeh

You might also like