UNIVERSITY ASSIGNMENT
Programming Fundamentals
Subject: Programming Fundamentals
Teacher Name: Mam Humra Owj
Student Name: Hasnat Raza
Semester: 2nd Semester
Date: May 14, 2026
Table of Contents
1. Introduction to Pointers
2. Pointers in C++ (Basics)
3. Pointers with Functions
4. Pointers with CString
5. Pointers with 1D Arrays
6. Conclusion
1. Introduction to Pointers
In C++ programming, pointers are very powerful. Normally, when a variable is created
(e.g., int x = 10;), the computer stores that value in a specific memory address. A
Pointer is a special variable that stores the address of another variable rather than a
standard value. This allows for direct memory manipulation, making programs more
efficient and faster.
2. Pointers in C++ (Basics)
Definition
A pointer is a variable that holds the memory address of another variable of the same data
type.
Key Symbols
• & (Address-of operator): Returns the memory address of a variable.
• * (Dereference operator): Used to declare a pointer and access the value at the
stored address.
Example Code
#include <iostream>
using namespace std;
int main() {
int num = 50;
int *ptr = # // Storing address of num in ptr
cout << "Value of num: " << num << endl;
cout << "Address of num: " << &num << endl;
cout << "Pointer ptr stores: " << ptr << endl;
cout << "Value at pointer ptr: " << *ptr << endl;
return 0;
}
Output:
Value of num: 50
Address of num: 0x61ff1c
Pointer ptr stores: 0x61ff1c
Value at pointer ptr: 50
3. Pointers with Functions
Passing pointers to a function is known as Pass by Reference. It allows a function to
modify the original variable's value in the main scope because it works directly with the
memory address.
void changeValue(int *p) {
*p = 100; // Changing value at the address
}
int main() {
int myNumber = 20;
changeValue(&myNumber); // Passing address
cout << "After function: " << myNumber; // Prints 100
return 0;
}
4. Pointers with CString
A CString is an array of characters ending with a null character (). In C++, the name of a
character array acts as a pointer to its first element.
char name[] = "Hasnat";
char *ptr = name;
cout << "String: " << ptr << endl;
cout << "First char: " << *ptr << endl;
cout << "Second char: " << *(ptr + 1) << endl;
5. Pointers with 1D Arrays
Array names are constant pointers to the first element. Any element arr[i] can be
accessed using pointer arithmetic as *(arr + i).
int arr[3] = {10, 20, 30};
int *p = arr;
for(int i = 0; i < 3; i++) {
cout << "Element " << i << ": " << *(p + i) << endl;
}
6. Conclusion
Pointers are a fundamental concept in C++ that bridge the gap between high-level logic
and low-level memory management. By understanding how to use addresses, developers
can write more optimized code for functions, arrays, and strings.
Submitted By: Hasnat Raza