0% found this document useful (0 votes)
9 views12 pages

Prog II Pointer Example Lab

The document provides a series of C++ lab examples that demonstrate various concepts related to pointers, including address and pointer operators, pointer arithmetic, and accessing elements in arrays and strings. Each example includes full code snippets that can be implemented in Dev C++, covering topics such as swapping numbers, string manipulation, and dynamic memory allocation. The examples aim to enhance understanding of pointer usage in C++ programming.

Uploaded by

estifanos haile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Prog II Pointer Example Lab

The document provides a series of C++ lab examples that demonstrate various concepts related to pointers, including address and pointer operators, pointer arithmetic, and accessing elements in arrays and strings. Each example includes full code snippets that can be implemented in Dev C++, covering topics such as swapping numbers, string manipulation, and dynamic memory allocation. The examples aim to enhance understanding of pointer usage in C++ programming.

Uploaded by

estifanos haile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

lab examples with full code that cover various concepts related to pointers, including the

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;

void swap(int a, int b) { int temp = *a; *a = *b; *b = temp; }

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; } ```

7. Reverse an array using pointers: ```cpp #include using namespace std;

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++:

Example 1: Address Operator ```cpp

include

using namespace std;

int main() { int num = 10; int* ptr = &num; // pointer variable storing the address of num

// Printing the address of num

cout << "Address of num: " << ptr << endl;

// Printing the value stored at the address pointed by ptr cout << "Value at the address pointed
by ptr: " << *ptr << endl;

return 0;

} ```

Example 2: Pointer Operator ```cpp

include
using namespace std;

int main() { int num = 10; int* ptr; // pointer variable

ptr = &num; // assigning the address of num to ptr

// Printing the value stored at the address pointed by ptr cout << "Value at the address pointed
by ptr: " << *ptr << endl;

return 0;

} ```

Example 3: Expression ```cpp

include

using namespace std;

int main() { int num1 = 10; int num2 = 5; int* ptr = &num1; // pointer variable storing the
address of num1

// Adding the value of num2 to the value pointed by ptr

*ptr += num2;

// Printing the updated value cout << "Updated value at the address pointed by ptr: " << *ptr
<< endl;

return 0;

} ```
Example 4: Arithmetic ```cpp

include

using namespace std;

int main() { int num = 10; int* ptr = &num; // pointer variable storing the address of num

// Incrementing the value pointed by ptr

(*ptr)++;

// Printing the updated value cout << "Updated value at the address pointed by ptr: " << *ptr
<< endl;

return 0;

} ```

Example 5: Strings ```cpp

include

using namespace std;

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

while (*ptr != '\0') {

cout << *ptr;

ptr++;

return 0;

} ```

Example 6: Arrays ```cpp

include

using namespace std;

int main() { int arr[] = {10, 20, 30, 40, 50}; int* ptr = arr; // pointer variable pointing to the
first element of arr

// Accessing array elements using pointer arithmetic

for (int i = 0; i < 5; i++) {

cout << "Value at index " << i << ": " << *(ptr + i) << endl;

return 0;

} ```
Example 7: Pointer to Pointer ```cpp

include

using namespace std;

int main() { int num = 10; int* ptr = &num; // pointer variable storing the address of num int
ptrToPtr = &ptr; // pointer to pointer variable storing the address of ptr

// Printing the value stored at the address pointed by ptrToPtr

cout << "Value at the address pointed by ptrToPtr: " << **ptrToPtr << endl;

return 0;

} ```

Example 8: Dynamic Memory Allocation ```cpp

include

using namespace std;

int main() { int* ptr = new int; // dynamically allocating memory for an integer

*ptr = 10; // storing a value in the dynamically allocated memory

// 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;

} ```

Example 9: Pointers and Functions ```cpp

include

using namespace std;

void square(int* num) { num = (*num) (*num); // squaring the value pointed by num }

int main() { int num = 5;

// Passing the address of num to the square function

square(&num);

// Printing the updated value of num cout << "Updated value of num: " << num << endl;

return 0;

} ```

Example 10: Pointers and Arrays as Function Arguments ```cpp

include

using namespace std;


void printArray(int* arr, int size) { for (int i = 0; i < size; i++) { cout << "Value at index " <<
i << ": " << *(arr + i) << endl; } }

int main() { int arr[] = {10, 20, 30, 40, 50};

// Passing the array and its size to the printArray function

printArray(arr, sizeof(arr) / sizeof(arr[0]));

return 0;

Example 1: Address Operator ```cpp

include

using namespace std;

int main() { int num = 10; int* ptr = &num; // pointer variable storing the address of num

// Printing the address of num

cout << "Address of num: " << ptr << endl;


// Printing the value stored at the address pointed by ptr cout << "Value at the address pointed
by ptr: " << *ptr << endl;

return 0;

} ```

Example 2: Pointer Arithmetic ```cpp

include

using namespace std;

int main() { int arr[] = {10, 20, 30, 40, 50}; int* ptr = arr; // pointer variable pointing to the
first element of arr

// Accessing array elements using pointer arithmetic

for (int i = 0; i < 5; i++) {

cout << "Value at index " << i << ": " << *(ptr + i) << endl;

return 0;

} ```

Example 3: String and Arrays ```cpp

include
using namespace std;

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

while (*ptr != '\0') {

cout << *ptr;

ptr++;

return 0;

You might also like