C programming - array size based on command line argument -
i'm trying make grid board game, know maximum size board can can smaller based on user inputs in command line. have made following program, compiles when write dimensions command line says 'segmentation fault (core dumped)'. can tell me i've done wrong?
#include <stdio.h> #include <stdlib.h> #include <string.h> #define board_width 80 #define board_height 52 int i; int j; int width; int height; int generations; int grid[board_width][board_height]; int main(int argc, char *argv[]) { if (argc < 2) { printf("not enough arguments entered\n"); exit(1); } else { width = atoi(argv[2]); height = atoi(argv[3]); generations = atoi(argv[4]); } for(i=0;i<width;i++) for(j=0;j<height;j++) printf("%2d", grid[i][j]); }
many things
you've set fixed board_width , board_height when declare variable, if pass in higher values on command line it's not going work.
but trying print? have not initialised grid
specific, printing out random memory.
firstly, you'll have initialise grid using 'new': here create 2d array variable sized dimensions
then, you'll have initialise variables something. can print them out.
it make easier if showed passed in program. of above reasons start.
also crash if don't pass in 3 arguements program, you're using 3.
although you're using argv[2] argv[4] - should using argv[1] argv[3]. in current state crash unless pass 4 arguements.
Comments
Post a Comment