1

I'm beginner in C and I'm trying to perform operations dynamically allocated to and read from a file input.txt. The problem is that I can not find the error that is causing the "segmentation fault". Will allocate the matrices and perform operations increasingly finding '+' or '-' signs. When you find '=' sign, I will print the result in a file output.txt.

My initial code is allocating the array like this:

#include <stdio.h>
#include <stdlib.h>

double ***AlocarMatriz(int p, int m, int n)
{
    int i, j;
    double ***Matriz;
    Matriz = (double***) malloc (p * sizeof(double**)); // alloc number of plans
    for (i = 0; i < p; i++){
        Matriz[i] = (double**) malloc (m * sizeof(double*));
        if (Matriz[i]==NULL)
            printf("plain"); // alloc rows
        for (j = 0; j < p; j++){
            Matriz[i][j] = (double*) malloc (n * sizeof(double));
            if (Matriz[i][j]==NULL)
                printf("plain"); // alloc cols
        }
    }
    return Matriz;
}

void ImprimeMatriz(double ***Matriz, int p, int m, int n)
{ // print matrices
    int i, j, k;
    for(i = 0; i < p; i++){
        for(j = 0; j < m; j++){
            printf("\n");
            for(k = 0; k < n; k++){
                printf("%.2lf ", Matriz[i][j][k]);
            }
        }
    }
}

int main(int argc, char *argv[])
{
    double*** A;
    int x, y, z;
    scanf("%d", &x);
    scanf("%d", &y);
    scanf("%d", &z);
    A = AlocarMatriz(x, y, z);
    //double A[x][y][z];

    ImprimeMatriz(A, x, y, z);

    system("PAUSE");


    return 0;
}

I tried to create a code to read the file and get the arrays that will be like this:

       2    //instances - number of operations
       2 2 2 //dimension of matrix - plan, row and column
       0 1   // first matrix
       1 0
       0 1
       1 0
       +   //operation
       0 1 //second matrix
       1 0
       0 1
       1 0
       =   //flag for stop and print result in output.txt file

My code for open txt file:

       FILE *fent;

//int m, n, p, i, j, k;


if (argc != 3) {                      
    fprintf(stderr, "Entry with correct params numbers!!!\n");
        exit(1);
}

fent = fopen (argv[1], "r");         //try open file
if (fent == NULL) {                  
    fprintf("Error to open %s for entry \n", argv[1]);
    return -1;
    }




fclose(fent);

return (EXIT_SUCCESS);

I'm not able to continue because I don't understand where I am going wrong. Tks for your help.

2
  • 2
    Your second for-loop in your allocator is wrong. This: for (j=0; j < p; j++) should be this: for (j = 0; j < m; j++) Commented Aug 30, 2013 at 23:31
  • Thanks WhozCraig. I'll make the changes. Commented Aug 31, 2013 at 10:45

1 Answer 1

2
for (j = 0; j < p; j++){
            Matriz[i][j] = (double*) malloc (n * sizeof(double));

should be :

 for (j = 0; j < m; j++){
            Matriz[i][j] = (double*) malloc (n * sizeof(double));

And hope you can initialize you memory before you use it.

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

1 Comment

Thanks folks. I'll make the changes.

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.