Storing # from .dat file in Matrix using Pointers

This is a discussion on Storing # from .dat file in Matrix using Pointers within the C Programming forums, part of the General Programming Boards category; I want my code to read matrix.dat and store the numbers in a 4x4 matrix using pointers. Then I would ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    24

    Storing # from .dat file in Matrix using Pointers

    I want my code to read matrix.dat and store the numbers in a 4x4 matrix using pointers. Then I would like to print the matrix line by line. Although my program compiles, I get a core dump error before it prints its result.
    This is my code:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    /* function prototypes */
    void prob1(void);
    void prob2(void);
    void gauss(int n, double **k, double *rhs);
    /* add here additional function prototypes as needed */
    
    main()
    {
            int menu;
            printf("Enter the function number to execute (1-2):");
            scanf("%d", &menu);
            switch(menu){
             case 1:
                prob1();
                break;
             case 2:
                prob2();
                break;
             default:
                printf("prob%d() does not exist.\n", menu);
            }
            exit(0);
    }
    
    void prob2(void)
    {
    FILE *Inf_1;
    double **A, *b;
    int row;
    char text[81];
    int k=0,t=0;
    
    b = (double *)malloc((size_t)3);
    A = (double **)calloc(16, sizeof(double *));
    
    
    Inf_1 = fopen("matrix.dat", "r");
    printf(" Put your solution for problem 2 here:\n");
    //By using a pointer to pointer **A and the function calloc()
    //allocate the memory for the 4x4 matrix A[][]
    //By using a pointer *b and the function malloc() allocate the memory
    //for the 4-dimensional vector b[]
    
    if(b==NULL)
    {
    printf("(main) malloc failed in double *b\n");
    exit(1);
    }
    if(A==NULL)
    {
    printf("(main) calloc failed in double **a\n");
    exit(2); 
    }
    
    
    //Read the components of A and b from the given input file matrix.dat.
    //The last line of the input file contains the components of b. The rows of
    //matrix A are the first four lines of the input file.
    
    
    
    if ((Inf_1 == NULL))
    {
    printf("Error opening the file\n");
    exit(1);
    }
    
    while(fgets(text,81,Inf_1)!=NULL)
    {
    
    if (k < 5) {
       sscanf(text, "%lf %lf %lf %lf", *A[t], *(A[t]+1), *(A[t]+2), *(A[t]+3));
    k++;
    t++;
    
       }
    
    else
    sscanf(text, "%lf %lf %lf %lf", *b, *(b+1), *(b+2), *(b+3));
    }
    
    
    //Print the components of A[][] row by row, and the components of b[].
    printf("\na[1][]= %lf %lf %lf %lf\n", *A[0],*(A[0]+1),*(A[0]+2),*(A[0]+3));
    printf("a[2][]= %lf %lf %lf %lf\n", *(A[1]),*(A[1]+1),*(A[1]+2),*(A[1]+3));
    printf("a[3][]= %lf %lf %lf %lf\n", *(A[2]),*(A[2]+1),*(A[2]+2),*(A[2]+3));
    printf("a[4][]= %lf %lf %lf %lf\n", *(A[3]),*(A[3]+1),*(A[3]+2),*(A[3]+3));
    
    
    }
    When I explicitly store the matrix data as arrays (ie &A[0][0], &A[0][1]...), I receive a result. Why am I getting core dumps with pointers?

    This is matrix.dat:
    Code:
    -1.0 1.0 -4.0 -0.5
     2.0 1.5  3.0  2.1
    -3.1 0.7 -2.5  4.2
     1.4 0.3  2.4 -1.9
     0.0 1.2 -3.0 -0.5

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    sscanf, as scanf, requires pointers. So you would do:
    Code:
    sscanf(text, "&#37;lf %lf %lf %lf", A[t], A[t]+1, A[t]+2, A[t]+3);
    or if you prefer
    Code:
    sscanf(text, "%lf %lf %lf %lf", &A[t][0], &A[t][1], &A[t][2],&A[t][3]);
    the second being more clear (for me)

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    This is my altered code:
    Code:
    if ((Inf_1 == NULL))
    {
    printf("Error opening the file\n");
    exit(1);
    }
    
    while(fgets(text,81,Inf_1)!=NULL)
    {
    
    if (k < 5) {
       sscanf(text, "&#37;lf %lf %lf %lf", &A[t][0], &A[t][1], &A[t][2], &A[t][3]);
    k++;
    t++;
    
       }
    
    else
    sscanf(text, "%lf %lf %lf %lf", b, b+1, b+2, b+3);
    }
    
    
    //Print the components of A[][] row by row, and the components of b[].
    printf("\n");
    for (k=0;k<4;k++) {
    printf("a[%d][] = %lf %lf %lf %lf\n",k+1,&A[k][0],&A[k][1],&A[k][2],&A[k][3]);
    }
    It looks much better but i'm still recieving a segmentation fault (core dumped) error. Any workarounds?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    b = (double *)malloc((size_t)3);
    ...
    sscanf(text, "%lf %lf %lf %lf", b, b+1, b+2, b+3);
    How many 8 byte double values can you fit in 3 bytes? Certainly not 4, I can guarantee it!


    Code:
    A = (double **)calloc(16, sizeof(double *));
    ...
       sscanf(text, "%lf %lf %lf %lf", &A[t][0], &A[t][1], &A[t][2], &A[t][3]);
    You are never allocating anything at the A[n] locations - you allocate 16 pointers to double. But they are all NULL (or at least, filled with zero-bytes - it may not match NULL in some architectures, but most likely it does in this case), and there is no memory to hold your actual data that scanf is trying to store.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    Quote Originally Posted by matsp View Post
    You are never allocating anything at the A[n] locations - you allocate 16 pointers to double. But they are all NULL (or at least, filled with zero-bytes - it may not match NULL in some architectures, but most likely it does in this case), and there is no memory to hold your actual data that scanf is trying to store.

    --
    Mats
    Not quite sure how this exactly works. Why am I not allocating anything at A[n] and why is there no memory to hold the sscanf data?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dakarn View Post
    Not quite sure how this exactly works. Why am I not allocating anything at A[n] and why is there no memory to hold the sscanf data?
    Right, you have a pointer to pointer to double A, which you allocate 16 pointers to double for. These pointers do not point to any memory. You need to do a loop of 16 to allocate some memory for each row of 16, don't you think?

    You may want to study various posts here about pointers, as well as perhaps going back over the area of pointers in your C book.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21