C Programming - Arrays

Exercise : Arrays - General Questions
1.
What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?
The element will be set to 0.
The compiler would report an error.
The program may crash if some important data gets overwritten.
The array size would appropriately grow.
Answer: Option
Explanation:

If the index of the array size is exceeded, the program will crash. Hence "option c" is the correct answer. But the modern compilers will take care of this kind of errors.

Example: Run the below program, it will crash in Windows (TurboC Compiler)

#include<stdio.h>

int main()
{
    int arr[2];
    arr[3]=10;
    printf("%d",arr[3]);
    return 0;
}

Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the Turbo-C Compiler (Windows) output.

Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better.


2.
What does the following declaration mean?
int (*ptr)[10];
ptr is array of pointers to 10 integers
ptr is a pointer to an array of 10 integers
ptr is an array of 10 integers
ptr is an pointer to array
Answer: Option
Explanation:
No answer description is available. Let's discuss.

3.
In C, if you pass an array as an argument to a function, what actually gets passed?
Value of elements in array
First element of the array
Base address of the array
Address of the last element of array
Answer: Option
Explanation:

The statement 'C' is correct. When we pass an array as a funtion argument, the base address of the array will be passed.