#include <stdio.h>
void printString(char str[]) {
printf("%s", str);
}
int main() {
char str[10] = "Hello";
printString(str);
return 0;
}
Correct Answer: Hello
Explanation: The function `printString` prints the entire string stored in the character array.
#include <stdio.h>
void func(char *str[]);
#include <stdio.h>
void func(char **str);
#include <stdio.h>
void func(char str[][]);
#include <stdio.h>
void func(char str[][10]);
Correct Answer:
#include <stdio.h>
void func(char *str[]);
Explanation: When passing an array of strings to a function, it’s typically represented as an array of pointers to characters.
#include <stdio.h>
void printStrings(char *str[]) {
for (int i = 0; str[i] != NULL; i++) {
printf("%s ", str[i]);
}
}
int main() {
char *arr[3] = {"One", "Two", "Three"};
printStrings(arr);
return 0;
}
Correct Answer: One Two Three
Explanation: The function `printStrings` prints each string in the array until it encounters a NULL pointer.
Correct Answer: The size of each dimension must be specified when passing a multidimensional array to a function.
Explanation: When passing a multidimensional array to a function, the size of each dimension must be specified except for the first dimension.
#include <stdio.h>
void printMatrix(int mat[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
printMatrix(arr, 2, 3);
return 0;
}
2 5
3 6
2
3
4
5
6
Correct Answer: 1 4
2 5
3 6
Explanation: The function `printMatrix` prints the elements of the 2D array row by row.