When i run my code on windows (CodeBlocks) everything works but when i run in on Linux it says stack smashing detected and doesnt print anything but the board; Here ius the code:
#include <stdio.h>
#include <stdlib.h>
void inimatri(char mat[7][7]){
int l=0,c=0;
while (l<8){
while (c<8){
mat[l][c]='.';
c=c+1;
}
c=0;
l=l+1;
}
mat[3][3]='O';
mat[3][4]='X';
mat[4][3]='X';
mat[4][4]='O';
mat[2][5]='O';
mat[5][2]='O';
mat[3][2]='O';
mat[5][4]='O';
}
void showmat (char mat[7][7]){
int l=0,c=0;
printf(" 0 1 2 3 4 5 6 7");
while (l<8){
printf( "\n%d",l);
while (c<8){
printf( " %c",mat[l][c]);
c=c+1;
}
c=0;
l=l+1;
}
}
int hord(char mat[7][7], int l, int c, char pecaac){
int legal;
if (c== 7){
return 0;
}
if (mat[l][c]!='.'){
return 0;
}
if (mat[l][c+1]==pecaac){
return 0;
}
while ((c+1)<8){
if (mat[l][c+1]=='.'){
return 0;
}
else if (mat[l][c+1]== pecaac){
return 1;
}
else{
legal= 0;
}
c=c+1;
}
return(legal);
}
int hore(char mat[7][7], int l, int c, char pecaac){
int legal=0;
if (c== 0){
return 0;
}
if (mat[l][c]!='.'){
return 0;
}
if (mat[l][c-1]==pecaac){
return 0;
}
while (0<=(c-1)){
if (mat[l][c-1]=='.'){
return 0;
}
else if (mat[l][c-1]== pecaac){
return 1;
}
else {
legal=0;
}
c=c-1;
}
return (legal);
}
int verb(char mat[7][7], int l, int c, char pecaac){
int legal;
if (l== 7){
return 0;
}
if (mat[l][c]!='.'){
return 0;
}
if (mat[l+1][c]==pecaac){
return 0;
}
while ((l+1)<8){
if (mat[l+1][c]=='.'){
return 0;
}
else if (mat[l+1][c]== pecaac){
return 1;
}
else{
legal=0;
}
l=l+1;
}
return (legal);
}
int verc(char mat[7][7], int l, int c, char pecaac){
int legal;
if (l== 0){
return 0;
}
if (mat[l][c]!='.'){
return 0;
}
if (mat[l-1][c]==pecaac){
return 0;
}
while (0<=(l-1)){
if (mat[l-1][c]=='.'){
return 0;
}
else if (mat[l-1][c]== pecaac){
return 1;
}
else{
legal = 0;
}
l=l-1;
}
return (legal);
}
int diads(char mat[7][7], int l, int c, char pecaac){
int legal;
if (l== 0 || c== 7){
return 0;
}
if (mat[l][c]!='.'){
return 0;
}
if (mat[l-1][c+1]==pecaac){
return 0;
}
while (0<=(l-1) && c+1<8){
if (mat[l-1][c+1]=='.'){
return 0;
}
else if (mat[l-1][c+1]== pecaac){
return 1;
}
else{
legal = 0;
}
l=l-1;
c=c+1;
}
return (legal);
}
int diadi(char mat[7][7], int l, int c, char pecaac){
int legal;
if (l== 7 || c== 7){
return 0;
}
if (mat[l][c]!='.'){
return 0;
}
if (mat[l+1][c+1]==pecaac){
return 0;
}
while ((l+1)<8 && (c+1)<8){
if (mat[l+1][c+1]=='.'){
return 0;
}
else if (mat[l+1][c+1]== pecaac){
return 1;
}
else{
legal = 0;
}
l=l+1;
c=c+1;
}
return (legal);
}
int diaes(char mat[7][7], int l, int c, char pecaac){
int legal;
if (l== 0 || c== 0){
return 0;
}
if (mat[l][c]!='.'){
return 0;
}
if (mat[l-1][c-1]==pecaac){
return 0;
}
while (0<=(l-1) && 0<=(c-1)){
if (mat[l-1][c-1]=='.'){
return 0;
}
else if (mat[l-1][c-1]== pecaac){
return 1;
}
else{
legal = 0;
}
l=l-1;
c=c-1;
}
return (legal);
}
int diaei(char mat[7][7], int l, int c, char pecaac){
int legal;
if (l== 7 || c== 0){
return 0;
}
if (mat[l][c]!='.'){
return 0;
}
if (mat[l+1][c-1]==pecaac){
return 0;
}
while ((l+1)<8 && 0<=(c-1)){
if (mat[l+1][c-1]=='.'){
return 0;
}
else if (mat[l+1][c-1]== pecaac){
return 1;
}
else{
legal = 0;
}
l=l+1;
c=c-1;
}
return (legal);
}
int clegal(char mat[7][7], int l, int c, char pecaac){
int ld,le,lc,lb;
int ldi, lds,lei,les;
int legal=0;
lb=verb(mat,l,c,pecaac);
lc=verc(mat,l,c,pecaac);
le=hore(mat,l,c,pecaac);
ld=hord(mat,l,c,pecaac);
ldi=diadi(mat,l,c,pecaac);
lds=diads(mat,l,c,pecaac);
lei=diaei(mat,l,c,pecaac);
les=diaes(mat,l,c,pecaac);
legal = lb+lc+le+ld+ldi+lds+lei+les;
printf("\n %d",legal);
if (legal==0){
printf("Jogada ilegal");
}
else{
return 1;
}
}
int main(int argc, char *argv[])
{
char matriz[7][7];
char pecaj, pecaai;
int x,y;
pecaj='X';
pecaai='O';
inimatri(matriz);
showmat(matriz);
x=6;
y=4;
clegal(matriz,x,y,pecaj);
return 0;
}
I tried to check if i was adding more numbers than the array can store but i dint find any problem, also searched for other solutions but couldnt find anything
In testing out your code as is on my Linux system, it indeed threw a smashing error. In reviewing your code, a couple of things seemed to stick out which seemed to contribute to ending with a smashing error.
First off, as is pointed out in the good comments above, there seems to be some confusion as to when array elements are starting at "1" or "0", and also it appears that the program really is intended to work with an "8 x 8" array and not a "7 x 7" array.
In reviewing the following "printf" statement, it is evident that an "8 x 8" array is intended to be used.
So with that in mind, I did a bit of refactoring of the program to set up an "8 x 8" array along with utilizing ranges from "0" to "7" to conform to the usual and customary index subscripts within "C" programs. Following is the refactored version of your program.
Some key items to point out.
With that, following is a sample output at the terminal on my Linux system.
Review this refactored code at your leisure and see if it meets the spirit of your project.