Friday, 21 February 2025

C Language Notes: Pointers with examples

 1. Introduction to Pointers:

A pointer in C is a variable that stores the memory address of another variable. Instead of holding a

data value itself, it holds the location of a variable in memory. This allows for indirect access and

manipulation of variables.

Syntax for Declaring Pointers:

type *pointer_name;

type: The data type of the variable the pointer will point to (e.g., int, float, char).

pointer_name: The name of the pointer.

2. Declaring and Initializing Pointers:

• Declaring a pointer:

• int *ptr; // Pointer to an integer

• Initializing a pointer:

• int x = 10;

• int *ptr = &x; // ptr stores the address of x

Address-of operator (&): Used to get the address of a variable.

3. Dereferencing Pointers:

Dereferencing a pointer means accessing the value stored at the address the pointer is pointing to.

Dereference operator (*): Used to access the value stored at the pointer's address.

• int x = 10;

• int *ptr = &x;

• printf("%d", *ptr); // Output: 10

4. Pointer Arithmetic:

Pointers can be incremented or decremented, which will move them to the next or previous memory

location of the same type. The amount the pointer moves depends on the size of the data type it is

pointing to.

• Incrementing a pointer:

• ptr++; // Moves the pointer to the next memory location of the same data type

• Decrementing a pointer:

• ptr--; // Moves the pointer to the previous memory location

5. Pointers and Arrays:

In C, the name of an array is a pointer to the first element of the array. Hence, arrays and pointers are

closely related.

int arr[] = {1, 2, 3};

int *ptr = arr; // ptr points to the first element of the array

printf("%d", *ptr); // Output: 1 (value at arr[0])

6. Pointers to Pointers:

A pointer to a pointer is a pointer that stores the address of another pointer.

int x = 10;

int *ptr1 = &x;

int **ptr2 = &ptr1;

printf("%d", **ptr2); // Output: 10

7. Functions and Pointers:

Pointers can be passed to functions, allowing functions to modify the actual values of variables.

#include <stdio.h>


void updateValue(int *ptr) {

*ptr = 20; // Modifies the value of the original variable

}


int main() {

int num = 10;

updateValue(&num);

printf("%d", num); // Output: 20

return 0;

}

Common Pointer Operations in C:

• Address-of operator (&): To get the address of a variable.

• Dereference operator (*): To get the value stored at a memory address.

• Pointer arithmetic: To increment or decrement pointers.

Programs with Solutions:

1. Program to Demonstrate Pointer Basics

#include <stdio.h>


int main() {

int num = 5;

int *ptr = &num;


printf("Value of num: %d\n", num);

printf("Address of num: %p\n", &num);

printf("Value at ptr (Dereferenced): %d\n", *ptr);

printf("Address stored in ptr: %p\n", ptr);


return 0;

}

Explanation:

• &num gives the address of num.

• *ptr dereferences the pointer and gives the value stored at the address ptr points to.


2. Program to Demonstrate Pointer Arithmetic

#include <stdio.h>


int main() {

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

int *ptr = arr;


printf("First element: %d\n", *ptr);

ptr++;

printf("Second element: %d\n", *ptr);

ptr++;

printf("Third element: %d\n", *ptr);


return 0;

}

Explanation:

• We use pointer arithmetic (ptr++) to access the next element in the array.

• ptr++ moves the pointer to the next integer (size of int).


3. Program to Swap Two Numbers Using Pointers

#include <stdio.h>


void swap(int *a, int *b) {

int temp;

temp = *a;

*a = *b;

*b = temp;

}


int main() {

int x = 5, y = 10;

printf("Before swap: x = %d, y = %d\n", x, y);

swap(&x, &y);

printf("After swap: x = %d, y = %d\n", x, y);


return 0;

}

Explanation:

• The swap function uses pointers to modify the actual values of x and y.

4. Program to Demonstrate Pointer to Pointer

#include <stdio.h>


int main() {


int x = 5;

int *ptr1 = &x;

int **ptr2 = &ptr1;


printf("Value of x: %d\n", x);

printf("Value using ptr1: %d\n", *ptr1);

printf("Value using ptr2: %d\n", **ptr2);


return 0;

}

• ptr2 is a pointer to the pointer ptr1. We can access the value of x by dereferencing ptr2

twice.

5. Program to Demonstrate Function with Pointer

#include <stdio.h>


void square(int *num) {

*num = (*num) * (*num);

}


int main() {

int n = 4;

square(&n);

printf("Squared value: %d\n", n); // Output: 16

return 0;

}

Explanation:

• The function square takes a pointer as an argument and modifies the original value of n

through dereferencing.

6. Program to Find Length of String Using Pointer

#include <stdio.h>


int stringLength(char *str) {

int length = 0;

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

length++;

str++;

}

return length;

}


int main() {

char str[] = "Hello, world!";

int length = stringLength(str);

printf("Length of the string: %d\n", length); // Output: 13

return 0;

}

• The function stringLength uses a pointer to traverse through the string and count the

number of characters until the null character \0 is encountered.


Sure! Here are some additional C programming exercises focusing on pointers, arrays, and memory

management for you to practice.

1. Program to Reverse an Array Using Pointers

#include <stdio.h>


void reverseArray(int *arr, int size) {

int *start = arr;

int *end = arr + size - 1;

int temp;


while (start < end) {


temp = *start;

*start = *end;

*end = temp;

start++;

end--;

}

}


int main() {

int arr[] = {1, 2, 3, 4, 5};

int size = sizeof(arr) / sizeof(arr[0]);


printf("Original Array: ");

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

printf("%d ", arr[i]);

}


reverseArray(arr, size);


printf("\nReversed Array: ");

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

printf("%d ", arr[i]);

}


return 0;

}

Explanation:

• The function reverseArray uses pointers start and end to reverse the elements of the array.


2. Program to Find Maximum and Minimum Using Pointers

#include <stdio.h>


void findMaxMin(int *arr, int size, int *max, int *min) {

*max = *min = *arr;

for (int i = 1; i < size; i++) {

if (*(arr + i) > *max) {

*max = *(arr + i);

}

if (*(arr + i) < *min) {

*min = *(arr + i);

}

}

}


int main() {

int arr[] = {5, 2, 9, 1, 5, 6};

int size = sizeof(arr) / sizeof(arr[0]);

int max, min;


findMaxMin(arr, size, &max, &min);


printf("Max: %d\n", max);

printf("Min: %d\n", min);


return 0;

}

Explanation:

• The function findMaxMin takes pointers to max and min values and updates them with the

maximum and minimum values of the array.


3. Program to Copy One String to Another Using Pointers

#include <stdio.h>


void copyString(char *src, char *dest) {

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

*dest = *src;

src++;

dest++;

}

*dest = '\0'; // Null-terminate the destination string

}


int main() {

char src[] = "Hello, World!";

char dest[50];


copyString(src, dest);


printf("Source String: %s\n", src);

printf("Destination String: %s\n", dest);


return 0;

}

Explanation:

• The function copyString copies characters from src to dest using pointer arithmetic.


4. Program to Concatenate Two Strings Using Pointers

#include <stdio.h>


void concatenateStrings(char *str1, char *str2, char *result) {

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

*result = *str1;

str1++;


result++;

}

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

*result = *str2;

str2++;

result++;

}

*result = '\0'; // Null-terminate the result string

}


int main() {

char str1[] = "Hello, ";

char str2[] = "World!";

char result[50];


concatenateStrings(str1, str2, result);


printf("Concatenated String: %s\n", result);


return 0;

}

Explanation:

• The function concatenateStrings concatenates two strings str1 and str2 into result using

pointers.


5. Program to Count Occurrences of a Character in a String Using Pointers

#include <stdio.h>


int countOccurrences(char *str, char ch) {

int count = 0;

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


if (*str == ch) {

count++;

}

str++;

}

return count;

}


int main() {

char str[] = "Programming in C is fun!";

char ch = 'g';


int count = countOccurrences(str, ch);


printf("Character '%c' occurs %d times in the string.\n", ch, count);


return 0;

}

Explanation:

• The function countOccurrences traverses the string and counts the occurrences of the

character ch.


6. Program to Swap Two Numbers Using Pointers Without a Temporary Variable

#include <stdio.h>


void swap(int *a, int *b) {

*a = *a + *b;

*b = *a - *b;

*a = *a - *b;

}


int main() {

int x = 10, y = 20;


printf("Before swap: x = %d, y = %d\n", x, y);


swap(&x, &y);


printf("After swap: x = %d, y = %d\n", x, y);


return 0;

}

Explanation:

• The function swap swaps the values of x and y without using a temporary variable, by using

mathematical operations with pointers.


7. Program to Reverse a String Using Pointers

#include <stdio.h>


void reverseString(char *str) {

char *start = str;

char *end = str;

char temp;


// Move the 'end' pointer to the last character

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

end++;

}

end--; // Move back to the last valid character


// Swap characters between start and end pointers

while (start < end) {


temp = *start;

*start = *end;

*end = temp;

start++;

end--;

}

}


int main() {

char str[] = "Hello";


printf("Original String: %s\n", str);

reverseString(str);

printf("Reversed String: %s\n", str);


return 0;

}

Explanation:

• The function reverseString uses two pointers to reverse the characters in the string in place.


8. Program to Allocate Memory Dynamically for an Array of Integers Using Pointers

#include <stdio.h>

#include <stdlib.h>


int main() {

int *arr;

int n;


printf("Enter the number of elements: ");

scanf("%d", &n);


// Dynamically allocate memory for n integers

arr = (int *)malloc(n * sizeof(int));


if (arr == NULL) {

printf("Memory allocation failed!\n");

return 1;

}


printf("Enter the elements:\n");

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

scanf("%d", arr + i); // Using pointer arithmetic

}


printf("Array elements are:\n");

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

printf("%d ", *(arr + i)); // Using pointer arithmetic

}


// Free dynamically allocated memory

free(arr);


return 0;

}

Explanation:

• The program dynamically allocates memory for an array of integers using malloc. It then

allows the user to input values, displays them, and frees the memory once done.


9. Program to Find Factorial of a Number Using Recursion and Pointers

#include <stdio.h>


int factorial(int *n) {


if (*n <= 1) {

return 1;

} else {

(*n)--;

return *n * factorial(n);

}

}


int main() {

int num = 5;


printf("Factorial of %d is %d\n", num, factorial(&num));


return 0;

}

Explanation:

• The factorial function calculates the factorial of a number recursively, using a pointer to

modify the value of n.


10. Program to Compare Two Strings Using Pointers

#include <stdio.h>


int compareStrings(char *str1, char *str2) {

while (*str1 != '\0' && *str2 != '\0') {

if (*str1 != *str2) {

return 0; // Strings are not equal

}

str1++;

str2++;

}

return (*str1 == *str2); // Both strings are equal if both pointers end at '\0'


}


int main() {

char str1[] = "hello";

char str2[] = "hello";


if (compareStrings(str1, str2)) {

printf("The strings are equal.\n");

} else {

printf("The strings are not equal.\n");

}


return 0;

}

Explanation:

• The function compareStrings compares two strings character by character and returns 1 if

they are equal, and 0 if they are not.

No comments:

Post a Comment