C Programming - Arrays - Discussion

Discussion Forum : Arrays - Point Out Correct Statements (Q.No. 3)
3.
Which of the following statements are correct about the program below?
#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 code is erroneous since the subscript for array used in for loop is in the range 1 to size.
The code is erroneous since the values of array are getting scanned through the loop.
The code is erroneous since the statement declaring array is invalid.
The code is correct and runs successfully.
Answer: Option
Explanation:

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().

Discussion:
8 comments Page 1 of 1.

Amrutha said:   7 years ago
@All. Please help me.

When I compile my program unexpected error is given but the answer is;

The code is erroneous since the statement declaring an array is invalid.
variable array size is allowed.so, they give segmentation faults.

Noel said:   7 years ago
No, The error is initializing the array at 1 instead of 0. If that is corrected it runs smoothly.

#include<stdio.h>

int main()
{
int size, i;
scanf("%d", &size);
int arr[size];
for(i=0; i<=size; i++)
{
scanf("%d\n", &arr[i]);
printf("%d\t",arr[i]);
}
return 0;
}

AKSHAY KALRA said:   8 years ago
Variable sized arrays are allowed.

The only wrong part in this question is for loop and scanf statement. It should be:-

for(i=1; i < size; i++)
{
scanf("%d", &arr[i]);
printf("%d", arr[i]);
}

Shubham_K said:   10 years ago
The code compiles successfully on GCC. But on running, it throws segmentation fault just because of the line 'scanf("%d", arr[i])'.

On replacing it with 'scanf("%d",(arr+1))' eliminates the segmentation fault too, and the code runs successfully.

Sireesh said:   1 decade ago
This program compiles without any errors on gcc compiler. It gives segmentation fault while running because 'scanf("%d", arr[i])' statement may access invalid memory location.

Mitali said:   1 decade ago
None of the option is correct. Code is erroneous just because in scanf '&' operator is not used. Else code works fine.

Meenu said:   1 decade ago
Variable sized arrays are allowed. This gives segmentation fault. So correct answer is:

A The code is erroneous since the subscript for array used in for loop is in the range 1 to size.

Bhath said:   1 decade ago
#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;
}
this working in gcc compiler

Post your comments here:

Your comments will be displayed after verification.