0% found this document useful (0 votes)
4 views

Lecture 7 Pointer

Uploaded by

painpath09
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 7 Pointer

Uploaded by

painpath09
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Pointer

Course Code: CSC 2106 Course Title: Introduction to Programming


(Theory)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 07 Week No: Semester:


Lecturer: Nazia Alfaz
[email protected]
Pointer
Variable

 The computer access its own memory not by using variable names but by using a memory map
where each location of memory is uniquely defined by a number, called the address. Pointers
are a very powerful, but primitive facility to avail that address. To understand pointer let us go
through the concept of variables once more.
 A variable is an area of memory that has been given a name. For example: int x; is an area of
memory that has been given the name x. The instruction x=10; stores the data value 10 in
the area of memory named x. The instruction &x returns the address of the location of variable
x.
 A pointer is a variable that stores the location of a memory/variable. A pointer has to be
declared. For example: int *p; Adding an asterisk (called the de-referencing operator) in
front of a variable's name declares it to be a pointer to the declared type.
 int *p; is a pointer – which can store an address of a memory location where an integer
value can be stored or which can store an address of the memory location of an integer
variable.
 For example: int *p , q; declares p, a pointer to int, and q an int and the instruction:
p=&q; stores the address of q in p. After this instruction, conceptually, p is pointing at q.
Variable

 After declaring a pointer *p variable, it can be used like any other variable.
That is, p stores the address or pointer, to another variable; &p gives the
address of the pointer variable itself; and *p is the value stored in the
variable that p points at.
&p represents the memory area of &x represents the memory area
variable named *p of variable named x
p represents the address stored inside the int x;
int *p; x represents the value stored
area of variable named *p
inside the area of variable named
*p represents the value stored in the area x
represented/pointed to by address p
Location of x:
Location of *p: &p = 1078 &x = 567
Value at x:
address at *p: p = &x = 567 x = 89

567 Value pointed by *p: *p = 89 89

Main Memory
Example

1 // Understanding pointer variable


2 void main( void )
3{
4 int x = 10;
5 int *p = &x;
6 int y = *p;
7 cout <<"Address of integer variable x: "<< &x <<"\n";
8 cout <<"Value stored in the memory area of x: "<< x <<"\n";
9 cout <<"Address of integer pointer variable *p: "<< &p <<"\n";
10 cout <<"Address stored in the area of pointer *p: "<< p<<"\n";
11 cout <<"Address of integer variable y: "<< &y <<"\n";
12 cout <<"Value pointed to by the pointer *p: "<< *p <<"\n";
13 cout <<"Value stored in the memory area of variable y: "<< y <<"\n";
14 }

Address of integer variable x: 0x8fbbfff0


Value stored in the memory area of x: 10
Address of integer pointer variable *p: 0x8fbbfff4
Address stored in the area of pointer *p: 0x8fbbfff0
Address of integer variable y: 0x8fbbfff8
Value pointed to by the pointer *p: 10
Value stored in the memory area of variable y: 10
Pointer & Array
An array is simply a block of memory. An array can be accessed with pointers as well as
with [] square brackets. The name of an array variable is a pointer to the first element in
the array. So, any operation that can be achieved by array subscripting can also be done
with pointers or vice-versa.
1 void main( void )
2{
3 float r[5] = {22.5,34.8,46.8,59.1,68.3};
4 cout <<"1st element: "<< r[0] <<"\n";
5 cout <<"1st element: "<< *r <<"\n";
6 cout <<"3rd element: "<< r[2] <<"\n";
7 cout <<"3rd element: "<< *(r+2)<<"\n";
1st element: 22.5
8 float *p; 1st element: 22.5
9 p = r; //&r[0] 3rd element: 46.8
10 cout <<"1st element: "<< p[0] <<"\n"; 3rd element: 46.8
1st element: 22.5
11 cout <<"1st element: "<< *p <<"\n"; 1st element: 22.5
12 cout <<"3rd element: "<< p[2]<<"\n"; 3rd element: 46.8
13 cout <<"3rd element: "<< *(p+2)<<"\n"; 3rd element: 46.8
14 for(int i=0; i<5; i++, p++) Element 1 is: 22.5
Element 2 is: 34.8
15 cout <<"Element "<<(i+1)<<" is: "<<*p<<"\n"; Element 3 is: 46.8
16 } Element 4 is: 59.1
Element 5 is: 68.3
Pointer & Array
 The array float r[5]; or the pointer variable *p (after p=r) is a pointer to the
first floating point number in the declared array.
 The 1st element of the array, 22.3, can be accessed by using: r[0], p[0], *r or *p.
 The 3rd element, 46.8, could be accessed by using: r[2], p[2],*(r+2) or
*(p+2).
 Now, let’s examine the notation (r+2) and (p+2).

&r[2] = r+2 r[2]=*(r+2)


r 123464
0 1 2 3 4
22.5 34.8 46.8 59.1 68.3
123456
&p[2] = p+2
p[2]=*(p+2)
p
 Assuming the starting address of the array numbers is 123456 –
r[0]=(r+0) starts at address, r+0*sizeof(float) = 123456 + 0 * 4 = 123456,
r[1]=(r+1) starts at address, r+1*sizeof(float) = 123456 + 1 * 4 = 123460,
r[2]=(r+2) starts at address, r+2*sizeof(float) = 123456 + 2 * 4 = 123464.
Void Pointer
#include<iostream>
using namespace std;

int main(){
void *p;  The void type of pointer is a special
//char a='A';
type of pointer which represents the
absence of type. So void pointers are
//int a=5;
pointers that point to a value that has no
double a=68.7; type (and thus also an undetermined
p=&a; length and undetermined dereference
if(sizeof(a)==1){
properties).
cout<<*((char*)p)<<endl;

}  This allows void pointers to point to any


data type, int, float, char, double
else if (sizeof(a)==4){
or any type of array.
cout<<*((int*)p)<<endl;
}
 But the data pointed by them cannot be
else if (sizeof(a)==8){
directly dereferenced, since we have no
}
cout<<*((double*)p)<<endl; type to dereference to.
else{
 So need to cast the address in the void
cout<<"Invalid"; pointer to some other pointer type that
}
points to a concrete data type before
}
dereferencing it.
Null Pointer

 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. This value
is the result of type-casting the integer value zero to any pointer type.

1 int * p;
2 p = 0; //can also write, p = NULL;
3 /* p has a null pointer value */

 Do not confuse null pointers with void pointers. A null pointer is a value that
any pointer may take to represent that it is pointing to "nowhere", while a void
pointer is a special type of pointer that can point to somewhere without a specific
type.

 One refers to the value stored in the pointer itself and the other to the type of data it
points to.
Dynamic Memory Allocation

 The exact size of array is unknown until the compile time, i.e., time when a compiler
compiles code written in a programming language into an executable form. The size
of array declared initially can be sometimes insufficient and sometimes more than
required. Also, what if we need a variable amount of memory that can only be
determined during runtime?

 Dynamic memory allocation allows a program to obtain more memory space, while
running or to release space when no space is required.

 C++ integrates the operators new and delete for dynamic memory allocation.
Dynamic Memory Allocation: Operators new And new[]

 In order to request dynamic memory we use the operator new. new is followed by a
data type specifier. If a sequence of more than one memory block is required, the
data type specifier is followed by the number of these memory blocks within
brackets []. It returns a pointer to the beginning of the new block of memory
allocated. Syntax:

1 pointer = new vtype;


2 pointer = new vtype [number_of_elements];

 The first expression is used to allocate memory to contain one single element of type
vtype. The second one is used to assign a block (an array) of elements of type
vtype, where number_of_elements is an integer value representing the
amount of these.
Dynamic Memory Allocation: Operators delete And delete[]

 Since the necessity of dynamic memory is usually limited to specific moments within
a program, once it is no longer needed it should be freed so that the memory
becomes available again for other requests of dynamic memory. This is the purpose
of the operator delete, whose format is:

1 delete pointer;
2 delete [] pointer;

 The first expression should be used to delete memory allocated for a single element,
and the second one for memory allocated for arrays of elements.

 The value passed as argument to delete must be either a pointer to a memory block
previously allocated with new, or a null pointer (in the case of a null pointer,
delete produces no effect).
Dynamic Memory Allocation & Deallocation: Example 2
#include<iostream>
using namespace std;

int main(){
int *p;
int n;
string rep;
int elem;

cout<<"Enter a number: ";


cin>>n;
cout<<"The entered number is: "<<n<<endl;
cout<<"Do you want to input more numbers ?"<<endl;
cin>>rep;

if(rep=="yes"){
cout<<"enter number of elements that you want to input: ";
cin>>elem;
p=new int [elem+1];
p[0]=n;

cout<<"Input "<<elem<<" numbers: ";


for(int i=1; i<=elem; i++){

cin>>p[i];
}
cout<<"Inputed Numbers are: ";
for(int i=0; i<=elem; i++){

cout<<p[i]<<" ";
}

else{
cout<<"Thank you"<<endl;
}
delete [] p;

You might also like