C Programming - Arrays
- Arrays - General Questions
- Arrays - Find Output of Program
- Arrays - Point Out Correct Statements
- Arrays - Yes / No Questions
#include<stdio.h>
int main()
{
int a[3][4];
fun(a);
return 0;
}
void fun(int p[][4]){ } is the correct way to write the function fun(). while the others are considered only the function fun() is called by using call by reference.
1: | When array name is used with the sizeof operator. |
2: | When array name is operand of the & operator. |
3: | When array name is passed to scanf() function. |
4: | When array name is passed to printf() function. |
The statement 1 and 2 does not yield the base address of the array. While the scanf() and printf() yields the base address of the array.
#include<stdio.h>
int main()
{
int size, i;
scanf("%d", &size);
int arr[size];
for(i=1; i<=size; i++)
{
scanf("%d", arr[i]);
printf("%d", arr[i]);
}
return 0;
}
The statement int arr[size]; produces an error, because we cannot initialize the size of array dynamically. Constant expression is required here.
Example: int arr[10];
One more point is there, that is, usually declaration is not allowed after calling any function in a current block of code. In the given program the declaration int arr[10]; is placed after a function call scanf().
int num[6];
num[6]=21;
The statement 'B' is correct, because int num[6]; specifies the size of array and num[6]=21; designates the particular element(7th element) of the array.
1: | The array int num[26]; can store 26 elements. |
2: | The expression num[1] designates the very first element in the array. |
3: | It is necessary to initialize the array at the time of declaration. |
4: | The declaration num[SIZE] is allowed if SIZE is a macro. |
1. The array int num[26]; can store 26 elements. This statement is true.
2. The expression num[1] designates the very first element in the array. This statement is false, because it designates the second element of the array.
3. It is necessary to initialize the array at the time of declaration. This statement is false.
4. The declaration num[SIZE] is allowed if SIZE is a macro. This statement is true, because the MACRO just replaces the symbol SIZE with given value.
Hence the statements '1' and '4' are correct statements.