Download Programming Fundamentals: Pointers - Lecture 10 and more Study notes Programming Languages in PDF only on Docsity!
Programming Fundamentals
Lecture-
Pointers
Today’s agenda
• Concept of pointers
• Working of pointers
• Common mistakes with pointers
• Void pointers
• Representing an array with pointers
• Using pointers with functions
- Here, 0x at the beginning represents the
address is in the hexadecimal form.
- Notice that the first address differs from the
second by 4 bytes and the second address
differs from the third by 4 bytes.
- This is because the size of an int variable is 4
bytes in a 64-bit system.
Declaring pointers
int *pointVar;
- Here, we have declared a pointer pointVar of the int type.
- We can also declare pointers in the following way.
int* pointVar; // preferred syntax
- Let's take another example of declaring pointers.
int* pointVar, p;
- Here, we have declared a pointer pointVar and a normal
variable p.
Note: The * operator is used after the data type to declare
pointers.
Get the Value from the Address Using
Pointers
- To get the value pointed by a pointer, we use the * operator. For
example:
int* pointVar, var; var = 5;
//assign address of var to pointVar
pointVar = &var;
//access value pointed by pointVar
cout << *pointVar << endl;
Output: 5
- In the above code, the address of var is assigned to pointVar. We
have used the *pointVar to get the value stored in that address.
- When * is used with pointers, it's called the dereference operator.
It operates on a pointer and gives the value pointed by the
address stored in the pointer. That is, *pointVar = var
Working of C++ Pointers
#include
using namespace std;
int main()
{
int var = 5;
// declare pointer variable
int* pointVar;
// store address of var
pointVar = &var;
// print value of var
cout << "var = " << var << endl; // print address of var cout << "Address of var (&var) = " << &var << endl << endl;
// print pointer pointVar
cout << "pointVar = " << pointVar << endl;
// print the content of the address pointVar points to
cout << "Content of the address pointed to by pointVar (*pointVar) = " << *pointVar << endl; return 0; }
Output var = 5 Address of var (&var) = 0x61ff pointVar = 0x61ff Content of the address pointed to by pointVar (*pointVar) = 5
Changing Value Pointed by
Pointers-examples
If pointVar points to the address of var, we can change the
value of var by using *pointVar. For example,
int var = 5; int* pointVar;
// assign address of var
pointVar = &var;
// change value at address pointVar
*pointVar = 1;
cout << var << endl;
Output: 1
- Here, pointVar and &var have the same address, the value
of var will also be changed when *pointVar is changed.
Common mistakes when working with
pointers
Suppose, we want a pointer varPoint to point to the address of var. Then, int var, *varPoint;
- // Wrong! varPoint is an address but var is not
varPoint = var;
- // Wrong! &var is an address and *varPoint is the value stored in &var
*varPoint = &var;
- // Correct! varPoint is an address and so is &var
varPoint = &var;
- // Correct! both *varPoint and var are values
*varPoint = var;
Solution-Void Pointers
// void pointer
void *ptr; double d = 9.0;
// valid code
ptr = &d;
The void pointer is a generic pointer that is used
when we don't know the data type of the
variable that the pointer points to.
C++ Void Pointer
#include
using namespace std;
int main()
void* ptr; float f = 2.3;
// assign float address to void
ptr = &f;
cout << &f << endl;
cout << ptr << endl;
return 0;
Output 0xffd117ac 0xffd117ac
Here, the pointer ptr is given the value of &f. The output shows that the void pointer ptr stores the address of a float variable f.
Point to Every Array Elements
Suppose we need to point to the fourth element of the array
using the same pointer ptr.
Here, if ptr points to the first element in the above example
then ptr + 3 will point to the fourth element. For example,
int *ptr;
int arr[5];
ptr = arr;
ptr + 1 is equivalent to &arr[1];
ptr + 2 is equivalent to &arr[2];
ptr + 3 is equivalent to &arr[3];
ptr + 4 is equivalent to &arr[4];
Similarly, we can access the elements using the
single pointer. For example, use dereference
operator
*ptr == arr[0];
*(ptr + 1) is equivalent to arr[1];
*(ptr + 2) is equivalent to arr[2];
*(ptr + 3) is equivalent to arr[3];
*(ptr + 4) is equivalent to arr[4];