Patratel Patratel - 4 years ago 94
C Question

Understanding the logic behind a piece of C code

I would love if you guys could explain to me why the following piece of recursive code doesn't print the word 'test'. Thanks in advance.

void drawTetriminosEachPosition(int **tetriminos, char **dBoard, int **tBoard, int i){
char c;
char **dBoard2;

if(tetriminos[i] == '\0')
{
return;
}
else
{
dBoard2 = dBoard;
DrawTetrimino(tBoard, tetriminos[i], dBoard, i+65);
}
i++;
return (drawTetriminosEachPosition(tetriminos,dBoard,tBoard,i));

ft_putstr("test");
if(checkChar(tBoard,tetriminos[i]))
{
dBoard = dBoard2;
return (drawTetriminosEachPosition(tetriminos,dBoard,tBoard,i));
}
}

Answer Source

Statements after a return are never executed. Since the first

return (drawTetriminosEachPosition(tetriminos,dBoard,tBoard,i));

does not depend on any condition, the following code is not executed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download