0

I'm new to C/C++ programming language. I want to make simple Conway's Game of Life using C programming language.

My code looks like this:

#include "stdafx.h"

// Define some constants
const int DIM = 10;
int board[DIM][DIM];

int checkNeighbours(int x, int y) {
    int left, right, top, bottom = 0;
    int sum;

    // Check neighbour cells
    if (board[x - 1][y]) { left = 1; }
    if (board[x + 1][y]) { right = 1; }
    if (board[x][y - 1]) { top = 1; }
    if (board[x][y + 1]) { bottom = 1; }

    sum = left + right + top + bottom;

    return sum;
}

// Build a board
void buildBoard() {
    int i, j, neighbour_count;

    for (i = 0; i < DIM; i++) {
        for (j = 0; j < DIM; j++){

            neighbour_count = checkNeighbours(i, j);

            // Underpopulation
            if (neighbour_count < 2) { board[i][j] = 0; }
            // Lives if nc is 2 or 3
            // Overpopulation
            if (neighbour_count > 3) {
                board[i][j] = 0;
            }

            // Revive 
            if (neighbour_count == 3) {
                board[i][j] = 1;
            }
        }
    }

    // Predefined board cells
    board[1][2] = 1;
    board[1][3] = 1;
    board[2][2] = 1;
}

void drawBoard() {
    int i, j;

    for (i = 0; i < DIM; i++) {
        for (j = 0; j < DIM; j++) {
            char figure= (board[i][j]) ? 'A' : '_';
            printf("%c", figure);
        }
        printf("\n");
    }
}

int main()
{
    buildBoard();
    drawBoard();

    getchar();
    return 0;
}

Errors I'm getting:

Run-Time Check Failure #3 - The variable 'left' is being used without being initialized.

Run-Time Check Failure #3 - The variable 'right' is being used without being initialized.

Run-Time Check Failure #3 - The variable 'top' is being used without being initialized.

How can I fix this, since I've already initialized those variables.

intelis
  • 7,829
  • 14
  • 58
  • 102
  • 2
    you haven't initialized those variables, that isnt how the comma operator works. – dwcanillas May 07 '15 at 20:09
  • 1
    @dwcanillas that's not a comma operator there. It is just comma used for initialization. – vsoftco May 07 '15 at 20:10
  • 1
    `int left, right, top, bottom = 0;` only initializes `bottom`, which is why it's the only one not mentioned in the error list. – Ken White May 07 '15 at 20:11
  • @vsoftco looked it up, you're correct. Its actually on [wikipedia](http://en.wikipedia.org/wiki/Comma_operator) but it notes that it isnt an operator there. TIL. – dwcanillas May 07 '15 at 20:25

1 Answers1

12
int left, right, top, bottom = 0; 

is initializing only the last variable. Better:

int left = 0 , right = 0, top = 0, bottom = 0;

or

int left, right, top, bottom;
left = right = top = bottom = 0;

Same flavour:

char* a, b, c, d; 

declares only a as char* pointer, rest of b, c and d are char.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • 1
    The last example becomes more clear when written as `char *a, b, c, d;` because `char*` is not a type. – Weather Vane May 07 '15 at 20:14
  • @WeatherVane yes, but, as I'm sure you know, there are two camps: pro `char* a` and pro `char *a` (especially when dealing with single parameters). I personally prefer `char* param` when writing the parameters of a function. The idea I think is that one shouldn't really mix such definitions. It's not too much of a hassle to write two lines. – vsoftco May 07 '15 at 20:16
  • 1
    Your example is a good hit for the latter *camp! Upvoted anyway. – Weather Vane May 07 '15 at 20:18
  • Yes I guess so :) Some rants: http://stackoverflow.com/q/398395/3093378 However, `char*` *is a type*. – vsoftco May 07 '15 at 20:18
  • I'm not going into whether there are any pointer types, but it's a good read thank you. – Weather Vane May 07 '15 at 20:24
  • @WeatherVane, Of course `char*` is a type. If you're arguing in the context of C++ and the standard says it's a type, then it's a type. You could argue that in some other context, and it could be useful there, but it would not carry through to this context. – chris May 07 '15 at 20:26
  • 1
    @chris I don't do C++. In a previous Q (can't find) I was told off for declaring multiple variables of the same type in one line with commas as being bad practice. But there's a balance between readabilty and strictness. Better to be clear, and separate the pointer var to another line. – Weather Vane May 07 '15 at 20:29
  • 1
    @WeatherVane, Yup, I definitely wouldn't recommend mixing pointer declarations and other declarations. I do one declaration per line in general. – chris May 07 '15 at 21:34