0

I'm trying to make a grid for a board game, i know the maximum size the board can be however it can also be smaller based on what the user inputs in the command line. I have made the following program, it compiles successfully but when i write the dimensions into the command line it says 'Segmentation fault (core dumped)'. Can anyone tell me what 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]);
}
masoud
  • 55,379
  • 16
  • 141
  • 208
Ibz
  • 1
  • 1
  • 2

4 Answers4

1

Many things

You've set a fixed BOARD_WIDTH and BOARD_HEIGHT when you declare the variable, so if you pass in higher values than that on the command line it's not going to work.

But mostly what are you trying to print? You have not initialised grid to anything specific, so you are printing out random memory.

Firstly, you'll have to initialise your grid by using 'new': Look here for that Create a 2D array with variable sized dimensions

Then, you'll have to actually initialise those variables to something. Then you can print them out.

It would make it easier to help you if you showed us what you passed in to the program. But all of the above reasons are a start.

Also it will crash if you don't pass in 3 arguements to the program, as you're using 3.

Although you're using argv[2] to argv[4] - You should be using argv[1] to argv[3]. So in its current state it will crash unless you pass 4 arguements.

Community
  • 1
  • 1
Salgar
  • 7,687
  • 1
  • 25
  • 39
  • ALSO, You're checking if argc < 2, but you're using 3! it should be if argc < 4 – Salgar May 14 '13 at 14:51
  • thanks iam using argv[1] to argv[3] now. Iam passing this into the program ' ./gol 7 6 5 ' expecting a grid of width 7 and height 6 to show up, i didnt realise i had to initialize the grid. sorry im new to programming. Im not sure what you mean by i have to initialize those variables to something before printing them out – Ibz May 14 '13 at 14:58
  • `grid` has file scope, so it's initialised to all 0s. `new` is C++, but the question is tagged C. – Daniel Fischer May 14 '13 at 15:07
  • @DanielFischer , oh right does that mean i dont have to initialise the grid, im looking to leave the whole grid blank as in spaces and print a border on the outsides – Ibz May 14 '13 at 15:18
  • @Ibz Spaces aren't 0s. Without explicit initialisation, `grid` is initialised to all 0s, so if you want anything else in it, you have to set it at some point. – Daniel Fischer May 14 '13 at 15:22
0

Make sure there is something in argv[2], argv[3], argv[4] before you use them. It probably should read if(argc < 5) { exit(1); }

edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47
0

You can have 2 approaches to your problem, first You can create the maximum size array and use only active elements, 2nd You can create array every time your game starts. For the 2nd option you need to provide input information for the game size. The first method is already implemented by you (as you have the static board table) if u want to create it every time game is started simply create it after changing string values to the integers:

a = atoi(argv[2])
b = atoi(argv[3])
i = int[a][b]
cerkiewny
  • 2,761
  • 18
  • 36
  • do you think the method i have started to use is easier – Ibz May 14 '13 at 15:00
  • The main difference between those methods indicates the usage cases. Your method is good if the table will be used often with different sizes during one program execution (the program will be faster as no additional memory allocation will be used), however if You are going to use table only once it is better to allocate it one time with the argument values. – cerkiewny May 14 '13 at 15:02
0

I am going to assume that the code you are presenting is just a snippet. If so, my suspicion is that your main issue is that you are addressing the wrong indices for your arguments. The first argument is at index one (the file name being at zero). argc then is the total number of arguments including the filename.

Assuming that all of the arguments you are using are presented in the code snippet, you should have a total of four indices with width, height, and generations being at indices 1, 2, and 3 respectively.

corahm
  • 141
  • 1
  • 5