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.