13 Pointers
13 Pointers
Introduction to Pointers
• When we declare a variable, some memory is
allocated for it.
– Example:
int* P;
int v1=99;
int* p= &v1;
cout<<“ P points to the value: “<<*p;
A Pointer Example
int v1 = 0;
v1 and *p1 now refer to
int* p1 = &v1; the same variable
*p1 = 42;
cout << v1 << endl;
cout << *p1 << endl;
Output:
42
42
Pointer Assignment
• Assignment operator ( = ) is used to assign value
of one pointer to another
Output:
3
4
Null Address
• Like a local variable, a pointer is assigned an
arbitrary address if you don’t initialize it.
• 0 is a pointer constant that represents the empty or
Null address
– Its value indicates that pointer is not pointing to a valid
object
– Cannot dereference a pointer whose value is Null
int *ptr = 0; OR int *ptr=NULL;
cout << *ptr << endl; // ERROR: ptr
// does not point to
// a valid int
Relationship between Arrays and Pointers
• Arrays and pointers are closely related:
void main()
{
int numbers[]={10,20,30,40,50};
cout<<numbers[0]<<endl; 10
cout<<numbers<<endl; Address e.g., &34234
cout<<*numbers<<endl; 10
cout<<*(numbers+1); 20
}
Arrays and Pointers
• Arrays and pointers are closely related:
– Array name is the starting address of the array
• Let p = A;
int iVar=5;
float fVar=4.3;
char cVar=‘Z’;
int* p1;
void* vp2;
p1 = &iVar; // Allowed
p1 = &fvar; // Not Allowed
P1 = &cVar; // Not Allowed
vp2 = &fvar; // Allowed
vp2 = &cVar; // Allowed
vp2 = &iVar; // Allowed
Strings As Pointers
• Another way of accessing a
contiguous chunk of memory, instead
of with an array, is with a pointer:
• Since we are talking about strings, which are
made up of characters, we'll be using
pointers to characters, or rather, char *s.
• However, pointers only hold an
address, they cannot hold all
the characters in a character
array.
Strings As Pointers
• This means that when we use a char *
to keep track of a string, the
character array containing the
string must already exist (having
been either statically- or
dynamically-allocated).
label @2000
------------------------------
| S | i | n | g | l | e | \0 |
------------------------------
label2 @3000
------------------------------------------
| M | a | r | r | i | e | d | \0 | | |
------------------------------------------
labelPtr @4000
--------
| 2000 |
--------
Strings As Pointers
Note: Since we assigned the pointer the address of
an array of characters, the pointer must be a
character pointer--the types must match.
cout << "Third char is: " << labelPtr[2] << endl;
It's important to remember that the only reason the pointer labelPtr allows us to access the label array is because we made
labelPtr point to it. Suppose, we do the following:
labelPtr = label2;
Now, no longer does the pointer labelPtr refer to label, but now to label2 as follows:
label2 @3000
------------------------------------------
| M | a | r | r | i | e | d | \0 | | |
------------------------------------------
labelPtr @4000
--------
| 3000 |
--------
So, now when we subscript using labelPtr, we are referring to characters in label2. The following:
cout << "Third char is: " << labelPtr[2] << endl;
...
char name[20];
char c = 'c';
char d = 'd';
char* const ptr1 = &c;
ptr1 = &d; // Not Allowed
int var1 = 0;
const int* ptr = &var1;
*ptr = 1; // Not Allowed
cout<<*ptr;
Pointer to String Constant
char* <string Literal>
Pointer to String Constant
Pointer to String Constant
Pointer to String Constant - Example
// Copying string using Pointers
void main()
{
int n = 5;
cout<<"Before call: n = "<<n<<endl;
func(&n);
cout<<"After call: n = "<<n<<endl;
}
Pass by Reference Pointers– Example1
c
Pass by Reference Pointers– Example2
c
void compDouble(int* Ar)
{
for(int i=0;i<10;i++)
{ *Ar=(*Ar)*2;
Ar++;
}
}
void main()
{ int Arr[10]={0,1,2,3,4,5,6,7,8,9};
compDouble(Arr);
for(int i=0;i<10;i++)
cout<<Arr[i]<<endl;
}
Pass by Reference Pointers– Example2
c
Class Exercises
• Find errors:
int ptr*;
int x, *ptr;
&x = ptr;
int x, *ptr;
*ptr = &x;
int x, *ptr;
ptr = &x;
ptr = 100; //Store 100 in x
cout<<x;
Class Exercises <continued…>
int numbers[]={10,20,30,40,50};
cout<<“Third element of the array is: “;
cout<<*(number+3);
float level;
int *float_ptr = &level;