Thread: Simple program has segmentation fault??

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    Simple program has segmentation fault??

    Hey,
    this is a simple program that reads the first number read in a file, and depending on it will display a certain message... If I try to printf the size, then it will display it with no problems, it is only when I apply the switch and if loop that I get segmentation error..?? help please!!

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
    FILE* data;
    data=fopen("Problem_File.dat","r");
    
    int i,j;
    int data_buffer[i][j]; /* Create array */
    int size; 
    int *pa;
    int x;
    int square;
    int block;
    
    fscanf(data,"%i", &(data_buffer)); /* Scan in Sudoku Values */ 
    pa=&(data_buffer[0][0]); /* Pointer points at contents at [0][0] */
    size=*pa; /* Put contents of pointer into (size)*/
    
    switch(size) /* Set messages & error messages for user */
    {
    case 1:
    printf("Error - A 1x1 Sudoku is pointless, my friend\n");
    return 0; 
    case 2:
    printf("Using a 2x2 Sudoku\n");
    break;
    case 3: 
    printf("Using a 3x3 Sudoku\n");
    break;
    case 4:
    printf("Using a 4x4 Sudoku\n");
    break;
    }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    i am also dont know the file open and read the datas from files, also i am curious about how the datas can be saved on a 2Dmatrix with using .txt .

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    the elements in the Problem_file.dat are saved into the array, this code worked yesterday, but I'm not sure what happened today and its giving me segment error...

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You are using i and j to declare an array, before they have been given a value.
    Code:
    for(i=0;i<9;i++) {
       for(j=0;j<9;j++) {
          fscanf(yourFilePointer, "%c", &data[i][j]);
       }
    }
    Would be my recommendation for this.
    Last edited by Adak; 12-04-2011 at 09:58 AM.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    why
    Code:
    pa=&(data_buffer[0][0]); /* Pointer points at contents at [0][0] */
    size=*pa; /* Put contents of pointer into (size)*/
    why not just
    Code:
    size=data_buffer[0][0]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault in simple code
    By livin in forum C Programming
    Replies: 7
    Last Post: 11-21-2011, 08:19 AM
  2. simple shared memory throwing segmentation fault
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 10-03-2010, 06:53 AM
  3. Help with Segmentation fault - in simple code
    By ramchan in forum C Programming
    Replies: 8
    Last Post: 03-01-2009, 09:07 AM
  4. simple server & client getting Segmentation fault error
    By blondieoubre in forum Linux Programming
    Replies: 2
    Last Post: 12-11-2007, 06:26 AM
  5. Replies: 7
    Last Post: 12-10-2004, 01:58 AM

Tags for this Thread