Correct Answer: A variable that stores the address of another variable
Explanation: A pointer in C is a variable that stores the memory address of another variable. It allows direct manipulation of memory and is a powerful feature for tasks like dynamic memory allocation.
Correct Answer: int *ptr;
Explanation: To declare a pointer in C, you use the syntax datatype *ptr; where datatype is the type of variable the pointer will point to.
Correct Answer: Returns the address of the variable
Explanation: In C, the ‘&’ operator is the address-of operator. It returns the memory address of the variable it’s applied to.
#include <stdio.h>
int main() {
int num = 10;
int *ptr = #
printf("%d", *ptr);
return 0;
}
Correct Answer: 10
Explanation: This code snippet declares an integer variable ‘num’, initializes it to 10, and then declares a pointer ‘ptr’ pointing to the address of ‘num’. The printf statement prints the value pointed to by ‘ptr’, which is 10.
Correct Answer: The program crashes
Explanation: Dereferencing a null pointer in C results in undefined behavior, often leading to a segmentation fault and program termination.
Correct Answer: malloc(sizeof(int))
Explanation: To dynamically allocate memory for an integer in C, you use the malloc function, specifying the size of the data type in bytes.
Correct Answer: Performing arithmetic operations on pointers
Explanation: Pointer arithmetic in C involves adding or subtracting integers to pointers to navigate through memory or arrays.
Correct Answer: Depends on the compiler
Explanation: The size of a pointer variable in C depends on the architecture and compiler being used.
Correct Answer: A pointer that points to nothing
Explanation: A null pointer in C is a pointer that does not point to any memory location.
Correct Answer: A pointer that points to any data type
Explanation: The ‘void’ pointer in C is a generic pointer type that can be used to point to objects of any data type. It cannot be dereferenced directly.
#include <stdio.h>
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;
printf("%d", *(ptr + 2));
Correct Answer: 30
Explanation: The expression *(ptr + 2) calculates the address of the third element in the array ‘arr’ (since pointer arithmetic automatically accounts for the size of the data type), which is 30.
Correct Answer: It allows adding and subtracting integers to pointers
Explanation: Pointer arithmetic in C allows adding or subtracting integers to pointers, enabling navigation through memory or arrays.
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr++));
printf("%d", *(++ptr));
return 0;
}
Correct Answer: 34
Explanation: The expression *(ptr++) first prints the value at ‘ptr’ (which is 1) and then increments ‘ptr’. The expression *(++ptr) then prints the value at the next position after the increment, which is 3.
Correct Answer: The number of elements between the two memory addresses
Explanation: Pointer subtraction in C returns the number of elements between the two memory addresses, taking into account the size of the data type.
Correct Answer: Pointer arithmetic is restricted to arrays only.
Explanation: Pointer arithmetic in C can be applied to any pointer, not just those pointing to arrays.
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr + 2;
printf("%d", *ptr);
return 0;
}
What will be the output of this code?
Correct Answer: 30
Explanation: The pointer ‘ptr’ is initialized to point to the third element of the array ‘arr’ (30), as pointer arithmetic adds the size of the data type (int) multiplied by 2 to ‘arr’.
#include <stdio.h>
char str[] = "Hello";
char *ptr = str + 2;
printf("%c", *ptr);
Correct Answer: l
Explanation: The pointer ‘ptr’ is initialized to point to the third character of the string “Hello” (index 2), which is ‘l’.
Correct Answer: Pointer arithmetic with void pointers is allowed, but the size of the data type must be specified.
Explanation: Pointer arithmetic with void pointers is allowed in C, but because void pointers have no associated data type, the size of the data type must be explicitly specified.
Correct Answer: The program compiles successfully but produces unpredictable results.
Explanation: In C, there are no runtime bounds checking for array accesses, so performing pointer arithmetic beyond the bounds of an array leads to unpredictable behavior.
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr1 = arr + 1;
int *ptr2 = arr + 3;
printf("%d", ptr2 - ptr1);
return 0;
}
Correct Answer: 1
Explanation: Pointer subtraction in C returns the number of elements between the two memory addresses. Here, ptr2 points to the fourth element and ptr1 points to the second element, so the difference is 1.
Correct Answer: Arrays automatically decay into pointers when passed to functions.
Explanation: In C, when an array is passed to a function, it decays into a pointer to its first element.
Correct Answer: The array is converted into a pointer to its first element.
Explanation: When an array decays into a pointer in C, it is automatically converted into a pointer to its first element.
#include <stdio.h>
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
What does ‘ptr’ point to?
Correct Answer: The first element of ‘arr’.
Explanation: ‘ptr’ points to the first element of the ‘arr’ array.
Correct Answer: arr[2]
Explanation: In C, array indexing starts from 0, so the third element is accessed using the index 2.
Correct Answer: By passing a pointer to the first element of the array.
Explanation: Arrays decay into pointers when passed to functions, so passing a pointer to the first element effectively passes the entire array.
#include <stdio.h>
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr + 3));
What will be the output of this code?
Correct Answer: 4
Explanation: *(ptr + 3) accesses the fourth element of the ‘arr’ array, which is 4.
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
printf("%d", *arr);
return 0;
}
Correct Answer: 1
Explanation: *arr dereferences the pointer to the first element of the ‘arr’ array, which is 1.
Correct Answer: Pointers and arrays are entirely distinct concepts in C.
Explanation: While arrays decay into pointers in certain contexts, pointers and arrays have different properties and behaviors in C.
Correct Answer: Using the sizeof operator on the array variable.
Explanation: sizeof operator returns the size of the entire array in bytes, so dividing by the size of each element gives the number of elements.
#include <stdio.h>
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[2];
printf("%d", *(ptr - 1));
What will be the output of this code?
Correct Answer: 2
Explanation: *(ptr – 1) accesses the element before the one pointed to by ‘ptr’, which is 2.
Correct Answer: No, arrays are fixed and cannot be reassigned.
Explanation: Arrays in C are fixed and cannot be reassigned to point to different memory locations after initialization.
#include <stdio.h>
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", ptr[2]);
What will be the output of this code?
Correct Answer: 3
Explanation: ptr[2] accesses the third element of the ‘arr’ array through pointer notation, which is 3.
Correct Answer: Arrays and pointers are identical when using the subscript operator.
Explanation: When using the subscript operator ([]), arrays and pointers are interchangeable in C.
#include <stdio.h>
int arr[] = {1, 2, 3, 4, 5};
int *ptr = &arr[0];
printf("%d", ptr[4]);
What will be the output of this code?
Correct Answer: 5
Explanation: ptr[4] accesses the fifth element of the ‘arr’ array through pointer notation, which is 5.
Correct Answer: There is no difference; they both access the same element.
Explanation: In C, arr[i] and *(arr + i) are equivalent and both access the element at index ‘i’ of the array ‘arr’.
#include <stdio.h>
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr + 4));
What will be the output of this code?
Correct Answer: 5
Explanation: *(ptr + 4) accesses the fifth element of the ‘arr’ array through pointer arithmetic, which is 5.
Correct Answer: Yes, but only if the pointers are of the same data type.
Explanation: In C, arrays of pointers can be created as long as the pointers are of the same data type.
#include <stdio.h>
int *ptr;
int arr[] = {1, 2, 3, 4, 5};
ptr = arr;
printf("%d", *ptr);
What will be the output of this code?
Correct Answer: 1
Explanation: *ptr accesses the value of the first element of the ‘arr’ array, which is 1.
#include <stdio.h>
char *ptr = "Hello";
printf("%c", *(ptr + 2));
Correct Answer: l
Explanation: *(ptr + 2) accesses the third character of the string “Hello”, which is ‘l’.
Correct Answer: Yes, always.
Explanation: In C, the name of an array can be used as a pointer to its first element in most contexts.
Correct Answer: As arrays of characters
Explanation: In C, strings are typically represented as arrays of characters, terminated by a null character (‘\0’).
Correct Answer: A string with a null character at the end
Explanation: A null-terminated string in C is a sequence of characters followed by a null character (‘\0’).
#include <stdio.h>
char str[] = "Hello";
char *ptr = str;
What does ‘ptr’ point to?
Correct Answer: The first character of ‘str’.
Explanation: ‘ptr’ points to the first character of the ‘str’ string.
#include <stdio.h>
char str[] = "Hello";
char *ptr = str;
printf("%c", *ptr);
Correct Answer: H
Explanation: *ptr accesses the value of the first character of the ‘str’ string, which is ‘H’.
#include <stdio.h>
char str[] = "Hello";
printf("%c", str[4]);
Correct Answer: o
Explanation: str[4] accesses the fifth character of the ‘str’ string, which is ‘o’.
#include <stdio.h>
char str[] = "Hello";
char *ptr = str;
printf("%s", ptr);
What will be the output of this code?
Correct Answer: Hello
Explanation: ‘%s’ format specifier in printf is used to print a null-terminated string, so it prints the entire ‘str’ string.
Correct Answer: The behavior is undefined.
Explanation: Modifying a string literal in C leads to undefined behavior, as string literals are typically stored in read-only memory.
#include <stdio.h>
char *str = "Hello";
printf("%c", *(str + 4));
What will be the output of this code?
Correct Answer: o
Explanation: *(str + 4) accesses the fifth character of the string “Hello”, which is ‘o’.
#include <stdio.h>
char str[] = "Hello";
char *ptr = str;
ptr += 2;
printf("%c", *ptr);
Correct Answer: l
Explanation: ‘ptr += 2;’ moves the pointer ‘ptr’ two positions forward in the ‘str’ string, so ‘*ptr’ now points to ‘l’.
Correct Answer: Using the ‘strcmp’ function.
Explanation: The ‘strcmp’ function in C is used to compare two strings lexically.
Correct Answer: To copy one string to another.
Explanation: The ‘strcpy’ function in C is used to copy the contents of one string to another.
#include <stdio.h>
char str1[] = "Hello";
char str2[10];
strcpy(str2, str1);
printf("%s", str2);
Correct Answer: Hello
Explanation: This code copies the content of ‘str1’ to ‘str2’ using ‘strcpy’ and then prints ‘str2’, which contains “Hello”.
Correct Answer: To concatenate two strings.
Explanation: The ‘strcat’ function in C is used to concatenate (append) one string to the end of another.
#include <stdio.h>
char str1[] = "Hello";
char str2[] = ", world!";
strcat(str1, str2);
printf("%s", str1);
Correct Answer: Hello, world!
Explanation: This code concatenates ‘str2’ to the end of ‘str1’ using ‘strcat’, resulting in “Hello, world!”.
Correct Answer: To find the length of a string.
Explanation: The ‘strlen’ function in C is used to determine the length of a string (the number of characters before the null terminator).
#include <stdio.h>
char str[] = "Hello";
int length = strlen(str);
printf("%d", length);
Correct Answer: 5
Explanation: ‘strlen’ returns the length of the string excluding the null terminator, so the length of “Hello” is 5.
Correct Answer: To copy one string to another with a specified length.
Explanation: The ‘strncpy’ function in C is used to copy characters from one string to another with a specified maximum length.
#include <stdio.h>
char str1[] = "Hello";
char str2[10];
strncpy(str2, str1, 3);
str2[3] = '\0';
printf("%s", str2);
What will be the output of this code?
Correct Answer: Hel
Explanation: ‘strncpy’ copies the first 3 characters of ‘str1’ to ‘str2’, and then ‘\0’ is added manually to terminate the string.
Correct Answer: To concatenate two strings with a specified length.
Explanation: The ‘strncat’ function in C is used to concatenate (append) a specified number of characters from one string to another.
#include <stdio.h>
char str1[] = "Hello";
char str2[] = ", world!";
strncat(str1, str2, 5);
printf("%s", str1);
What will be the output of this code?
Correct Answer: Hello, w
Explanation: ‘strncat’ appends the first 5 characters of ‘str2’ to ‘str1’, resulting in “Hello, w”.
Correct Answer: A pointer that points to the address of another pointer.
Explanation: A pointer to a pointer in C holds the address of another pointer.
#include <stdio.h>
int num = 10;
int *ptr = #
int **ptr_to_ptr = &ptr;
What does ‘ptr_to_ptr’ point to?
Correct Answer: The address of ‘ptr’.
Explanation: ‘ptr_to_ptr’ holds the address of ‘ptr’.
#include <stdio.h>
int num = 10;
int *ptr = #
int **ptr_to_ptr = &ptr;
printf("%d", **ptr_to_ptr);
Correct Answer: 10
Explanation: **ptr_to_ptr dereferences the pointer twice, accessing the value pointed to by ‘ptr’, which is 10.
Correct Answer: To create multi-dimensional arrays dynamically.
Explanation: Pointers to pointers are commonly used to create and manipulate multi-dimensional arrays dynamically.
#include <stdio.h>
int num = 10;
int *ptr = #
int **ptr_to_ptr = &ptr;
**ptr_to_ptr = 20;
printf("%d", num);
What will be the output of this code?
Correct Answer: 20
Explanation: **ptr_to_ptr modifies the value pointed to by ‘ptr’ indirectly, so ‘num’ becomes 20.
#include <stdio.h>
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
int **ptr_to_ptr = &ptr;
printf("%d", *(*ptr_to_ptr + 2));
Correct Answer: 3
Explanation: *(*ptr_to_ptr + 2) accesses the third element of the ‘arr’ array through pointer notation, which is 3.
#include <stdio.h>
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
int **ptr_to_ptr = &ptr;
*ptr_to_ptr += 2;
printf("%d", **ptr_to_ptr);
What will be the output of this code?
Correct Answer: 3
Explanation: *ptr_to_ptr += 2 moves the pointer ‘ptr’ two positions forward in the ‘arr’ array, so **ptr_to_ptr now points to 3.
Correct Answer: To enable the function to modify the original pointer.
Explanation: Passing a pointer to a pointer allows a function to modify the original pointer itself, not just the value it points to.
#include <stdio.h>
void changePtr(int **ptr) {
int num = 20;
*ptr = #
}
int main() {
int num = 10;
int *ptr = #
changePtr(&ptr);
printf("%d", *ptr);
return 0;
}
Correct Answer: 20
Explanation: The ‘changePtr’ function modifies the pointer ‘ptr’ to point to the local variable ‘num’, so the output is 20.
Correct Answer: To create multi-dimensional arrays.
Explanation: Double pointers are commonly used to create and manipulate multi-dimensional arrays, especially dynamically allocated ones.
Correct Answer: int **ptr;
Explanation: A double pointer in C is declared with two asterisks, such as int **ptr;.
#include <stdio.h>
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
int **ptr_to_ptr = &ptr;
printf("%d", **ptr_to_ptr);
What will be the output of this code?
Correct Answer: 1
Explanation: **ptr_to_ptr dereferences the pointer twice, accessing the value pointed to by ‘ptr’, which is the first element of ‘arr’ (1).
Correct Answer: To enable the function to modify the original pointer.
Explanation: Using a double pointer as a function argument allows the function to modify the original pointer, not just the value it points to.
#include <stdio.h>
void modifyPtr(int **ptr) {
int num = 20;
*ptr = #
}
int main() {
int num = 10;
int *ptr = #
modifyPtr(&ptr);
printf("%d", *ptr);
return 0;
}
What will be the output of this code?
Correct Answer: 20
Explanation: The ‘modifyPtr’ function modifies the pointer ‘ptr’ to point to the local variable ‘num’, so the output is 20.
#include <stdio.h>
int num1 = 10, num2 = 20;
int *ptr1 = &num1, *ptr2 = &num2;
int **ptr_to_ptr = &ptr1;
*ptr_to_ptr = ptr2;
printf("%d", **ptr_to_ptr);
Correct Answer: 20
Explanation: *ptr_to_ptr = ptr2; modifies ‘ptr1’ to point to ‘num2’, so **ptr_to_ptr will print the value of ‘num2’.
#include <stdio.h>
int num1 = 10, num2 = 20;
int *ptr1 = &num1, *ptr2 = &num2;
int **ptr_to_ptr = &ptr1;
**ptr_to_ptr += 5;
printf("%d", *ptr1);
Correct Answer: 15
Explanation: **ptr_to_ptr += 5; increments the value pointed to by ‘ptr1’ by 5, so *ptr1 will be 15.
#include <stdio.h>
int num1 = 10, num2 = 20;
int *ptr1 = &num1, *ptr2 = &num2;
int **ptr_to_ptr = &ptr1;
*ptr_to_ptr += 2;
printf("%d", **ptr_to_ptr);
What will be the output of this code?
Correct Answer: Compiler Error
Explanation: Incrementing a double pointer like ‘*ptr_to_ptr += 2;’ is not allowed in C.
#include <stdio.h>
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int **ptr = (int **)arr;
printf("%d", *(*(ptr + 1) + 1));
Correct Answer: 4
Explanation: *(ptr + 1) moves to the second row, and then +1 moves to the second column of that row, accessing 4.
#include <stdio.h>
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int **ptr = (int **)arr;
printf("%d", *(*(ptr + 2) + 1));
What will be the output of this code?
Correct Answer: 8
Explanation: *(ptr + 2) moves to the third row, and then +1 moves to the second column of that row, accessing 8.
#include <stdio.h>
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int **ptr = (int **)arr;
printf("%d", *(*(ptr + 1) + 2));
Correct Answer: 6
Explanation: *(ptr + 1) moves to the second row, and then +2 moves to the third column of that row, accessing 6.
Correct Answer: A pointer that points to the address of a function.
Explanation: A function pointer in C holds the address of a function.
Correct Answer: void (*ptr)();
Explanation: The syntax for declaring a function pointer is return_type (*ptr)(parameters).
#include <stdio.h>
void greet() {
printf("Hello, world!");
}
int main() {
void (*ptr)();
ptr = &greet;
ptr();
return 0;
}
What will be the output of this code?
Correct Answer: Hello, world!
Explanation: The function pointer ‘ptr’ is assigned the address of the ‘greet’ function, which is then called.
Correct Answer: To pass the address of a function.
Explanation: Function pointers in C allow passing the address of a function as an argument or return value.
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*ptr)(int, int);
ptr = &add;
printf("%d", ptr(3, 4));
return 0;
}
What will be the output of this code?
Correct Answer: 7
Explanation: The function pointer ‘ptr’ is assigned the address of the ‘add’ function, which is then called with arguments 3 and 4.
#include <stdio.h>
void printHello() {
printf("Hello");
}
void printWorld() {
printf(", world!");
}
int main() {
void (*ptr)();
ptr = &printHello;
ptr();
ptr = &printWorld;
ptr();
return 0;
}
Correct Answer: Hello, world!
Explanation: The function pointer ‘ptr’ is first assigned the address of ‘printHello’ and then ‘printWorld’, resulting in both functions being called.
Correct Answer: To dynamically select which function to call at runtime.
Explanation: An array of function pointers allows selecting and calling different functions based on runtime conditions.
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int main() {
int (*ptr[2])(int, int) = {&add, &subtract};
printf("%d", ptr[1](5, 3));
return 0;
}
What will be the output of this code?
Correct Answer: 2
Explanation: ‘ptr[1](5, 3)’ calls the ‘subtract’ function, resulting in 5 – 3 = 2.
#include <stdio.h>
void printNumber(int num) {
printf("%d", num);
}
void printChar(char ch) {
printf("%c", ch);
}
int main() {
void (*ptr[2])(int) = {&printNumber, &printChar};
ptr ;
ptr[1]('A');
return 0;
}
Correct Answer: 5A
Explanation: ‘ptr ‘ calls ‘printNumber’ with argument 5, and ‘ptr[1](‘A’)’ calls ‘printChar’ with argument ‘A’.
#include <stdio.h>
void multiply(int a, int b) {
printf("%d", a * b);
}
void divide(int a, int b) {
printf("%d", a / b);
}
int main() {
void (*ptr[2])(int, int) = {&multiply, ÷};
ptr[1](10, 5);
return 0;
}
Correct Answer: 2
Explanation: ‘ptr[1](10, 5)’ calls ‘divide’ with arguments 10 and 5, resulting in 10 / 5 = 2.
#include <stdio.h>
void printSquare(int num) {
printf("%d", num * num);
}
void printCube(int num) {
printf("%d", num * num * num);
}
int main() {
void (*ptr[2])(int) = {&printSquare, &printCube};
ptr ;
ptr ;
return 0;
}
Correct Answer: 39127
Explanation: ‘ptr ‘ calls ‘printSquare’ with argument 3, and ‘ptr ‘ calls ‘printCube’ with argument 3.
#include <stdio.h>
void addOne(int *num) {
(*num)++;
}
void subtractOne(int *num) {
(*num)--;
}
int main() {
int num = 5;
void (*ptr[2])(int *) = {&addOne, &subtractOne};
ptr[0](&num);
ptr[1](&num);
printf("%d", num);
return 0;
}
Correct Answer: 5
Explanation: ‘ptr[0](&num)’ calls ‘addOne’ with the address of ‘num’, and ‘ptr[1](&num)’ calls ‘subtractOne’ with the address of ‘num’. The value of ‘num’ remains 5.
#include <stdio.h>
void doubleValue(int *ptr) {
*ptr *= 2;
}
void halveValue(int *ptr) {
*ptr /= 2;
}
int main() {
int num = 10;
void (*ptr[2])(int *) = {&doubleValue, &halveValue};
ptr[0](&num);
ptr[1](&num);
printf("%d", num);
return 0;
}
Correct Answer: 10
Explanation: ‘ptr[0](&num)’ doubles the value of ‘num’, and ‘ptr[1](&num)’ halves the value of ‘num’. The value of ‘num’ remains 10.
#include <stdio.h>
void increment(int *ptr) {
(*ptr)++;
}
void decrement(int *ptr) {
(*ptr)--;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
void (*ptr[2])(int *) = {&increment, &decrement};
ptr[0](arr + 2);
ptr[1](arr + 1);
printf("%d", arr[3]);
return 0;
}
Correct Answer: 4
Explanation: ‘ptr[0](arr + 2)’ increments the value at index 2 of ‘arr’, and ‘ptr[1](arr + 1)’ decrements the value at index 1 of ‘arr’. So, arr[3] becomes 4.
#include <stdio.h>
void addTwo(int *ptr) {
(*ptr) += 2;
}
void subtractThree(int *ptr) {
(*ptr) -= 3;
}
int main() {
int num = 10;
void (*ptr[2])(int *) = {&addTwo, &subtractThree};
ptr[0](&num);
ptr[1](&num);
printf("%d", num);
return 0;
}
Correct Answer: 9
Explanation: ‘ptr[0](&num)’ adds 2 to ‘num’, and ‘ptr[1](&num)’ subtracts 3 from ‘num’. So, the final value of ‘num’ is 9.
#include <stdio.h>
void addFive(int *ptr) {
(*ptr) += 5;
}
void subtractTen(int *ptr) {
(*ptr) -= 10;
}
int main() {
int num = 20;
void (*ptr[2])(int *) = {&addFive, &subtractTen};
ptr[1](&num);
ptr[0](&num);
printf("%d", num);
return 0;
}
Correct Answer: 15
Explanation: ‘ptr[1](&num)’ subtracts 10 from ‘num’, and ‘ptr[0](&num)’ adds 5 to the result. So, the final value of ‘num’ is 15.
Correct Answer: To enable functions to modify arguments passed to them.
Explanation: Pointers allow functions to modify the values of arguments passed to them directly.
#include <stdio.h>
void swap(int *a, int *b) {
int 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;
}
What will be the output of this code?
After swap: x = 10, y = 5
After swap: x = 5, y = 10
After swap: x = 5, y = 10
After swap: x = 10, y = 5
Correct Answer: Before swap: x = 5, y = 10
After swap: x = 10, y = 5
Explanation: The ‘swap’ function exchanges the values of ‘x’ and ‘y’ using pointers.
#include <stdio.h>
void modify(int *ptr) {
(*ptr)++;
}
int main() {
int num = 5;
printf("Before modification: %d\n", num);
modify(&num);
printf("After modification: %d\n", num);
return 0;
}
After modification: 5
After modification: 6
After modification: 5
After modification: 6
Correct Answer: Before modification: 5
After modification: 6
Explanation: The ‘modify’ function increments the value of ‘num’ using a pointer.
#include <stdio.h>
void change(int *ptr) {
*ptr = 100;
}
int main() {
int num = 50;
change(&num);
printf("%d", num);
return 0;
}
Correct Answer: 100
Explanation: The ‘change’ function modifies the value of ‘num’ to 100 using a pointer.