Thread: Core dump using scanf in a function for arrays bigger than a certain length

1. Core dump using scanf in a function for arrays bigger than a certain length

Good morning guys
I have tried to write a function which allows to enter an array with scanf(). The array must be bidimensional, something like array[R][3] where R is a length decided by the user, 3 is the number of columns. To allocate memory for the array, I have to use malloc, and it seems to work fine. As you can see in the code below, which is not the final code but contains the function, the function must return the matrix created. So, for R values under 5, the program works. For R=5 and bigger, it returns "segmentation fault (core dump)", and I'd like to understand why this happens.
Code:
```#include<stdlib.h>
#include<stdio.h>
float **MatriceInput(int R);
int main(void){
int i,R;
float **M;
printf("Enter the number of lines\n");
scanf("%d",&R);
M=MatriceInput(R);
for(i=0;i<R;i++){
printf("%f\t%f\t%f\n",**(M+i),*(*(M+i)+1),*(*(M+i)+2));
}
}

float **MatriceInput(int R){
int i,j;
float **A;

A=(float **)malloc(R*sizeof(float));
if(A==NULL){
printf("First malloc failed\n");
}

for(i=0;i<R;i++){
A[i]=(float *)malloc(3*sizeof(float));
if(A[i]==NULL){
printf("Second malloc failed\n");
}
}
for(j=0;j<3;j++){
printf("Enter the column %d\n",j+1);
for(i=0;i<R;i++){
scanf("%f",&*(*(A+i)+j));
}
}
return(A);
}```

2. Step 1 is indent your code.
Code:
```#include<stdlib.h>
#include<stdio.h>

float **MatriceInput(int R);

int main(void)
{
int i, R;
float **M;
printf("Enter the number of lines\n");
scanf("%d", &R);
M = MatriceInput(R);
for (i = 0; i < R; i++) {
printf("%f\t%f\t%f\n", **(M + i), *(*(M + i) + 1), *(*(M + i) + 2));
}
}

float **MatriceInput(int R)
{
int i, j;
float **A;

A = (float **) malloc(R * sizeof(float));
if (A == NULL) {
printf("First malloc failed\n");
}

for (i = 0; i < R; i++) {
A[i] = (float *) malloc(3 * sizeof(float));
if (A[i] == NULL) {
printf("Second malloc failed\n");
}
}
for (j = 0; j < 3; j++) {
printf("Enter the column %d\n", j + 1);
for (i = 0; i < R; i++) {
scanf("%f", &*(*(A + i) + j));
}
}
return (A);
}```
2. Don't cast the return result of malloc in a C program.
Casting malloc - Cprogramming.com
If you're getting 'can't cast void*' errors, then you need to stop trying to compile your C program with a C++ compiler.
Renaming your source files to prog.c would be a good way.

3. Get the sizes right.
A = (float **) malloc(R * sizeof(float));
You're supposed to be allocating pointers here, not floats.
Eg.
A = malloc(R * sizeof(float*));

Or more generally, if you use this form, you never have to look back at the type.
p = malloc(sizeof(*p));

4. Normal array syntax works with pointers as well, so there's no need for a mess of * and ()

Code:
```float **MatriceInput(int R)
{
int i, j;
float **A;

A = malloc(R * sizeof(*A));
if (A == NULL) {
printf("First malloc failed\n");
}

for (i = 0; i < R; i++) {
A[i] = malloc(3 * sizeof(*A[i]));
if (A[i] == NULL) {
printf("Second malloc failed\n");
}
}

for (j = 0; j < 3; j++) {
printf("Enter the column %d\n", j + 1);
for (i = 0; i < R; i++) {
scanf("%f", &A[i][j]);
}
}
return (A);
}```
Extra bonus points for using 'row' and 'col' instead of i,j would further improve things.

3. Thanks for your clear and detailed answer, the function works properly now

4. Originally Posted by GiulioDag01
Thanks for your clear and detailed answer, the function works properly now