KEMBAR78
C Pointer Practice Questions Explained | PDF | Pointer (Computer Programming) | Computer Engineering
0% found this document useful (0 votes)
19 views4 pages

C Pointer Practice Questions Explained

The document provides a series of C pointer practice questions categorized into easy, medium, and hard levels, each accompanied by explanations. It covers fundamental concepts such as pointer declaration, pointer arithmetic, dynamic memory allocation, and advanced topics like function pointers and memory leak detection. Each example illustrates practical applications of pointers in C programming.

Uploaded by

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

C Pointer Practice Questions Explained

The document provides a series of C pointer practice questions categorized into easy, medium, and hard levels, each accompanied by explanations. It covers fundamental concepts such as pointer declaration, pointer arithmetic, dynamic memory allocation, and advanced topics like function pointers and memory leak detection. Each example illustrates practical applications of pointers in C programming.

Uploaded by

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

C Pointer Practice Questions with Explanations

EASY LEVEL
1. Basic Pointer Declaration
int a = 10;

int *p = &a;

printf("Value: %d", *p);

Explanation: We declare an integer variable 'a' and a pointer 'p' that holds the address of 'a'. Using *p, we access the

value stored at that address, which is 10.

2. Print Address and Value


int x = 5;

int *ptr = &x;

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

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

Explanation: This prints the memory address of variable 'x' and its value using a pointer. %p prints the address; *ptr

prints the value at that address.

3. Swap using Pointers


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

int temp = *a;

*a = *b;

*b = temp;

Explanation: Swaps two numbers by passing their addresses. We dereference the pointers to access and swap their

actual values.

4. Pointer Arithmetic
int arr[] = {1, 2, 3};

int *p = arr;

printf("%d", *(p + 1));

Explanation: Here, 'p' points to the start of the array. *(p + 1) accesses the second element of the array using pointer

arithmetic.

5. Access Array using Pointers


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

printf("%d ", *(arr + i));

Explanation: This loop accesses each element of the array using pointer notation instead of traditional indexing (arr[i]).

MEDIUM LEVEL
6. Function Returning Pointer
int* max(int *a, int *b) {

return (*a > *b) ? a : b;

Explanation: Returns the address of the larger of two integers by comparing their values using dereferencing.

7. Pointer to Pointer
int x = 10;

int *p = &x;

int **pp = &p;

**pp = 20;

Explanation: Double pointers (**pp) are used to modify the original variable by accessing it through two levels of

indirection.

8. Dynamic Memory Allocation


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

Explanation: Allocates memory for an integer array of size n using malloc. Always free this memory after use to avoid

memory leaks.

9. String Length using Pointers


int strlen_ptr(char *s) {

int len = 0;

while (*s++) len++;

return len;

Explanation: A custom strlen function using a pointer. It traverses the string until the null terminator is found,

incrementing the length.

10. Reverse Array using Pointers


void reverse(int *a, int n) {

int *end = a + n - 1;
while (a < end) {

int t = *a;

*a++ = *end;

*end-- = t;

Explanation: Reverses an array in-place using two pointers. One starts from the beginning and the other from the end.

HARD LEVEL
11. Double Pointer for 2D Array
int **arr = malloc(rows * sizeof(int*));

for (int i = 0; i < rows; i++)

arr[i] = malloc(cols * sizeof(int));

Explanation: Allocates memory for a 2D array using double pointers. First, allocate rows, then columns for each row.

12. Pointer to Structure


struct Student { int id; };

struct Student s = {1};

struct Student *ptr = &s;

printf("%d", ptr->id);

Explanation: Demonstrates accessing structure members using a pointer with the arrow operator (->).

13. Function Pointer


int add(int a, int b) { return a + b; }

int (*fp)(int, int) = add;

printf("%d", fp(2, 3));

Explanation: Stores the address of the function 'add' in a pointer and calls it using the function pointer.

14. Command Line Arguments


int main(int argc, char *argv[]) {

for (int i = 0; i < argc; i++)

printf("%s\n", argv[i]);

Explanation: Prints each command-line argument using argv, which is an array of character pointers.

15. Memory Leak Detection


int *p = malloc(10 * sizeof(int));

// forgot to free(p); -> memory leak!

Explanation: Allocates memory but does not free it. This results in a memory leak. Always use free() after malloc().

You might also like