0

try to get number of rows and column from user through array but it gives Segmentation fault at run time

#include<stdio.h>
int main(){
    int rows;
    int column;
    int arr[rows];
    int arr1[column];
    
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    printf("Enter the number of column: ");
    scanf("%d",&column);
    printf("\n");
    
int i=0;
while( i<rows)
{  printf("\n");
   printf("Enter the value of rows index: " );
   scanf("%d",&arr[i]);
   printf("\n");
    i++;
}
int j=0;
while(j<column)
{
   printf("Enter the value of rows index: " );
   scanf("%d",&arr1[j]);
   printf("\n");
    j++;
}
}

// giving Segmentation fault

1 Answer 1

1

At the time of your definition for the "arr" and "arr1" arrays, the value of column and rows is undefined.

int rows;
int column;
int arr[rows];
int arr1[column];

Move the declaration of those arrays after you have received input from the user.

printf("Enter the number of rows: ");
scanf("%d",&rows);
printf("Enter the number of column: ");
scanf("%d",&column);
printf("\n");
int arr[rows];
int arr1[column];

Give that a try and see if that addresses your segmentation fault.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.