Thread: why do i get segmentation fault here?and how do i fix it?

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    2

    why do i get segmentation fault here?and how do i fix it?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int printboard( int **board,int N);
        
    int main()
    {
        int i,j;
        int N=5;
      
        
        int board[N][N];
      
        
        for(i=0;i<N;i++){
            for(j=0;j<N;j++){
                board[i][j]=1;
            }
        }
        
            
        printboard( board,N);   
        return 0;
    }
    
    
    
    
    int printboard( int **board,int N){
        int i,j;
        for(i=0;i<N;i++){
            for(j=0;j<N;j++){
                printf("%d",board[i][j]);
            }
        }
    }
    Attached Images Attached Images why do i get segmentation fault here?and how do i fix it?-jpg 

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
     
    #define SIZE 5
     
    void printboard(int board[][SIZE]);
     
    int main() {
        int board[SIZE][SIZE];
     
        for (int i = 0; i < SIZE; i++)
            for (int j = 0; j < SIZE; j++)
                board[i][j] = 1;
     
        printboard(board);
     
        return 0;
    }
     
    void printboard(int board[][SIZE]) {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++)
                printf("%2d ", board[i][j]);
            putchar('\n');
        }
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    2
    thing is i need to get the size from the console with argv.i just simplified the problem so you can send me an easy fix

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The issue you have is that when an array is passed as an argument, it is converted to a pointer to its first element. This means that an array of arrays is converted to a pointer to an array, not a pointer to a pointer. This is why john.c's example retains the SIZE in the parameter.

    Note that your array of arrays is actually a variable length array, whereas you probably want a fixed size array as shown by john.c.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    If you really want to use a variable-size array, then maybe something like this.
    I also randomized the values.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
     
    void printboard(int Size, int board[][Size]) {
        for (int i = 0; i < Size; i++) {
            for (int j = 0; j < Size; j++)
                printf("%2d ", board[i][j]);
            putchar('\n');
        }
    }
     
    int main(int argc, char **argv) {
        int Size = 5;
     
        if (argc > 2) {
            fprintf(stderr, "Usage: board [SIZE]\n");
            return EXIT_FAILURE;
        } 
     
        if (argc == 2) Size = atoi(argv[1]);
     
        if (Size <= 0) {
            fprintf(stderr, "The size must be > 0\n");
            return EXIT_FAILURE;
        }
     
        int board[Size][Size];
       
        for (int i = 0; i < Size; i++)
            for (int j = 0; j < Size; j++)
                board[i][j] = rand() % 2;
         
        printboard(Size, board);
     
        return 0;
    }
    Last edited by john.c; 01-28-2020 at 03:12 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by laserlight View Post
    Note that your array of arrays is actually a variable length array, whereas you probably want a fixed size array as shown by john.c.
    Interesting. I didn't know this!
    I prefer to deal with multidimensional arrays as an argument through a single pointer:
    Code:
    void f(int size, int *ptr)
    {
      int i, j, *p;
      for ( i = 0; i < size; i++ )
        for ( j = 0; j < size; j++ )
        {
          p = ptr + size * j + i; // p points to "ptr[i][j]".
          ...
        }
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > why do i get segmentation fault here?and how do i fix it?
    You read the error message and fix the code, before trying to run it.

    That warning that was printed in your screen shot wasn't a slap on the wrist, it was a gun at your head.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Segmentation fault: 11
    By pilsang0512 in forum C Programming
    Replies: 4
    Last Post: 11-26-2013, 12:08 AM
  3. another segmentation fault
    By bazzano in forum C Programming
    Replies: 8
    Last Post: 10-01-2005, 10:20 AM
  4. segmentation fault
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 09-29-2005, 02:13 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread