Course: CS-303 Programming Fundamentals
Topics: Memory Addresses, Pointer Variables, Address-of Operator and Dereferencing
After this lecture, students will be able to:
&).*).Whenever we declare a variable in C++, the computer reserves memory to store its value.
For example:
int age = 20;
The computer stores the value 20 in a memory location.
Each memory location has an address.
Think of computer memory like a house:
Example: Variable in Memory
Memory address
Stored value
Illustrative memory address only
Variable: age
The actual memory address is assigned by the computer. It may be different every time the program runs.
A memory address is a location in computer memory where a variable is stored.
We can display the address of a variable using the address-of operator &.
#include <iostream>
using namespace std;
int main()
{
int age = 20;
cout << "Age: " << age << endl;
cout << "Address: " << &age << endl;
return 0;
}
Possible Output:
Age: 20
Address: 0x7ffe1234
The address shown above is only an example. Your computer will display a different address.
| Expression | Meaning |
|---|---|
age |
Value of the variable |
&age |
Memory address of the variable |
20 |
Value stored in the variable |
Remember: The & operator is called the address-of operator when used with a variable.
A pointer is a variable that stores the memory address of another variable.
In simple words:
Normal Variable
age
Stores a value
Pointer Variable
ptr
Stores an address
The general syntax of a pointer is:
data_type *pointer_name;
Examples:
int *ptr;
float *fptr;
char *cptr;
double *dptr;
Explanation:
int *ptr declares a pointer to an integer.float *fptr declares a pointer to a float.char *cptr declares a pointer to a character.double *dptr declares a pointer to a double.A pointer should point to a compatible data type.
We can store the address of a variable in a pointer using the address-of operator &.
#include <iostream>
using namespace std;
int main()
{
int age = 20;
int *ptr = &age;
cout << "Age: " << age << endl;
cout << "Address of age: " << &age << endl;
cout << "Pointer value: " << ptr << endl;
return 0;
}
Possible Output:
Age: 20
Address of age: 0x7ffe1234
Pointer value: 0x7ffe1234
int age = 20;
Creates an integer variable.
int *ptr = &age;
Creates a pointer and stores the address of age.
Notice that:
ptr == &age
Both expressions represent the same address.
Dereferencing means accessing the value stored at the memory address held by a pointer.
We use the * operator to dereference a pointer.
#include <iostream>
using namespace std;
int main()
{
int age = 20;
int *ptr = &age;
cout << "Value of age: " << age << endl;
cout << "Value using pointer: " << *ptr << endl;
return 0;
}
Output:
Value of age: 20
Value using pointer: 20
| Expression | Meaning |
|---|---|
age |
Value of age |
&age |
Address of age |
*ptr |
Value stored at the address held by ptr |
Important: The symbol * has two uses in these examples:
int *ptr, it declares a pointer.*ptr, it dereferences a pointer.A pointer can also modify the original variable.
#include <iostream>
using namespace std;
int main()
{
int number = 10;
int *ptr = &number;
cout << "Before modification: " << number << endl;
*ptr = 50;
cout << "After modification: " << number << endl;
return 0;
}
Output:
Before modification: 10
After modification: 50
Initially:
number = 10;
The pointer stores the address of number.
*ptr = 50;
This changes the value stored at that address.
Therefore, the original variable number becomes 50.
| Normal Variable | Pointer Variable |
|---|---|
| Stores a value | Stores a memory address |
Example: int x = 10; |
Example: int *ptr = &x; |
| Access value using variable name | Access pointed value using *ptr |
Address obtained using &x |
Address stored in ptr |
Consider this program:
int number = 25;
int *ptr = &number;
*ptr = 40;
Dry run:
| Step | Statement | number |
ptr |
|---|---|---|---|
| 1 | int number = 25; |
25 | Not declared |
| 2 | int *ptr = &number; |
25 | Address of number |
| 3 | *ptr = 40; |
40 | Address of number |
Final value:
number = 40;
Incorrect:
int *ptr;
*ptr = 20;
The pointer does not have a valid address. Dereferencing it causes undefined behavior.
Correct:
int number = 10;
int *ptr = &number;
*ptr = 20;
Incorrect:
float number = 10.5;
int *ptr = &number;
Correct:
float number = 10.5;
float *ptr = &number;
Incorrect:
int *ptr = nullptr;
cout << *ptr;
A null pointer does not point to a valid object. Dereferencing it causes undefined behavior.
Correct:
int *ptr = nullptr;
if (ptr != nullptr)
{
cout << *ptr;
}
Write a C++ program to display the value and memory address of an integer variable.
Tasks:
number.&.Requirements:
Write a C++ program to access an integer variable through a pointer.
Tasks:
number.number in the pointer.Requirements:
Write a C++ program to modify a variable using a pointer.
Tasks:
Requirements:
Write a C++ program to swap two numbers using pointers.
Tasks:
Requirements:
& operator?* operator?ptr and *ptr?nullptr?Test your understanding
0 / 6 answered
1. What does a normal integer variable store?
A. Memory address
B. Integer value
C. Function name
D. Pointer type
2. What does a pointer store?
A. Only decimal values
B. A memory address
C. A function output
D. A string
3. Which operator returns the address of a variable?
A. *
B. &
C. %
D. !
4. What does *ptr do?
A. Gets the value at the address stored in ptr
B. Gets the address of ptr
C. Deletes the variable
D. Declares a function
5. What happens when we execute *ptr = 50?
A. The pointer address becomes 50
B. The pointed variable gets the value 50
C. The pointer is deleted
D. The program always stops
6. Which value represents a null pointer?
A. 0.5
B. nullptr
C. true
D. 50
Submit Answers
In this lecture, we learned:
| Concept | Meaning |
|---|---|
| Memory Address | Location of a variable in memory |
| Pointer | Variable that stores an address |
& |
Address-of operator |
* |
Dereference operator |
nullptr |
Represents a null pointer |
Key Message:
A normal variable stores a value, while a pointer stores the memory address of another variable. Pointers allow us to access and modify data through memory addresses.