Prog II Pointer Example Lab
Prog II Pointer Example Lab
address operator, pointer operator, expressions, arithmetic, strings, and arrays. These
examples can be implemented using Dev C++:
1. Swap two numbers using pointers: ```cpp #include using namespace std;
int main() { int x = 5, y = 10; cout << "Before swapping: x = " << x << ", y = " << y << endl;
swap(&x, &y); cout << "After swapping: x = " << x << ", y = " << y << endl; return 0; } ```
2. Access array elements using pointers: ```cpp #include using namespace std;
int main() { int arr[] = { 1, 2, 3, 4, 5 }; int* ptr = arr; cout << "Array elements: "; for (int i =
0; i < 5; i++) { cout << *(ptr + i) << " "; } cout << endl; return 0; } ```
3. Find the length of a string using pointers: ```cpp #include using namespace std;
int stringLength(const char* str) { int length = 0; while (*str) { length++; str++; } return
length; }
int main() { const char* str = "Hello, World!"; cout << "Length of the string: " <<
stringLength(str) << endl; return 0; } ```
4. Concatenate two strings using pointers: ```cpp #include using namespace std;
void stringConcatenate(char str1, const char str2) { while (*str1) { str1++; } while (*str2)
{ *str1 = *str2; str1++; str2++; } *str1 = '\0'; }
int main() { char str1[50] = "Hello"; const char* str2 = ", World!"; cout << "Before
concatenation: " << str1 << endl; stringConcatenate(str1, str2); cout << "After concatenation:
" << str1 << endl; return 0; } ```
5. Access array elements using pointer arithmetic: ```cpp #include using namespace std;
int main() { int arr[] = { 1, 2, 3, 4, 5 }; int* ptr = arr; cout << "Array elements: "; for (int i =
0; i < 5; i++) { cout << *(ptr++) << " "; } cout << endl; return 0; } ```
6. Find the sum of elements in an array using pointers: ```cpp #include using namespace
std;
int arraySum(int* arr, int size) { int sum = 0; for (int i = 0; i < size; i++) { sum += *(arr +
i); } return sum; }
int main() { int arr[] = { 1, 2, 3, 4, 5 }; int size = sizeof(arr) / sizeof(arr[0]); int sum =
arraySum(arr, size); cout << "Sum of array elements: " << sum << endl; return 0; } ```
void reverseArray(int* arr, int size) { int* start = arr; int* end = arr + size - 1; while (start <
end) { int temp = *start; *start = *end; *end = temp; start++; end--; } }
int main() { int arr[] = { 1, 2, 3, 4, 5 }; int size = sizeof(arr) / sizeof(arr[0]); cout << "Before
reversing: "; for (int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl;
reverseArray(arr, size); cout << "After reversing: "; for (int i = 0; i < size; i++) { cout <<
arr[i] << " "; } cout << endl; return 0; } ```
8. Access array elements using pointer expressions: ```cpp #include using namespace
std;
int main() { int arr[] = { 1, 2, 3, 4, 5 }; int* ptr = arr; cout << "Array elements: "; for (int i =
0; i < 5; i++) { cout << ptr[i] << " "; } cout << endl; return 0; } ```
9. Increment array elements using pointers: ```cpp #include using namespace std;
void incrementArray(int* arr, int size) { for (int i = 0; i < size; i++) { (*arr)++; arr++; } }
int main() { int arr[] = { 1, 2, 3, 4, 5 }; int size = sizeof(arr) / sizeof(arr[0]); cout << "Before
increment: "; for (int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl;
incrementArray(arr, size); cout << "After increment: "; for (int i = 0; i < size; i++) { cout <<
arr[i] << " "; } cout << endl; return 0; } ```
10. Access 2D array elements using pointers: ```cpp #include using namespace std;
int main() { int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int* ptr = *arr; cout << "2D
Array elements: "; for (int i = 0; i < 9; i++) { cout << *(ptr + i) << " "; } cout << endl; return
0; } ```
0; } ```
Second lab examples covering various concepts related to pointers in C++:
include
int main() { int num = 10; int* ptr = # // pointer variable storing the address of num
// Printing the value stored at the address pointed by ptr cout << "Value at the address pointed
by ptr: " << *ptr << endl;
return 0;
} ```
include
using namespace std;
// Printing the value stored at the address pointed by ptr cout << "Value at the address pointed
by ptr: " << *ptr << endl;
return 0;
} ```
include
int main() { int num1 = 10; int num2 = 5; int* ptr = &num1; // pointer variable storing the
address of num1
*ptr += num2;
// Printing the updated value cout << "Updated value at the address pointed by ptr: " << *ptr
<< endl;
return 0;
} ```
Example 4: Arithmetic ```cpp
include
int main() { int num = 10; int* ptr = # // pointer variable storing the address of num
(*ptr)++;
// Printing the updated value cout << "Updated value at the address pointed by ptr: " << *ptr
<< endl;
return 0;
} ```
include
int main() { char str[] = "Hello, World!"; char* ptr = str; // pointer variable pointing to the
first character of str
// Printing individual characters of the string using pointer arithmetic
ptr++;
return 0;
} ```
include
int main() { int arr[] = {10, 20, 30, 40, 50}; int* ptr = arr; // pointer variable pointing to the
first element of arr
cout << "Value at index " << i << ": " << *(ptr + i) << endl;
return 0;
} ```
Example 7: Pointer to Pointer ```cpp
include
int main() { int num = 10; int* ptr = # // pointer variable storing the address of num int
ptrToPtr = &ptr; // pointer to pointer variable storing the address of ptr
cout << "Value at the address pointed by ptrToPtr: " << **ptrToPtr << endl;
return 0;
} ```
include
int main() { int* ptr = new int; // dynamically allocating memory for an integer
// Printing the value stored in the dynamically allocated memory cout << "Value in the
dynamically allocated memory: " << *ptr << endl;
delete ptr; // freeing the dynamically allocated memory
return 0;
} ```
include
void square(int* num) { num = (*num) (*num); // squaring the value pointed by num }
square(&num);
// Printing the updated value of num cout << "Updated value of num: " << num << endl;
return 0;
} ```
include
return 0;
include
int main() { int num = 10; int* ptr = # // pointer variable storing the address of num
return 0;
} ```
include
int main() { int arr[] = {10, 20, 30, 40, 50}; int* ptr = arr; // pointer variable pointing to the
first element of arr
cout << "Value at index " << i << ": " << *(ptr + i) << endl;
return 0;
} ```
include
using namespace std;
int main() { char str[] = "Hello, World!"; char* ptr = str; // pointer variable pointing to the
first character of str
ptr++;
return 0;