这段代码是大型源代码的一部分,但错误就在这里。
int solve(int* unsolved_var)
{
int i;
for (i = 0; unsolved_var[i] != 0; i++)
{
int n;
for (n = 1; n <= 9; n++)
{
//if (!numInBox(unsolved_var[i], n)==1 || !numInLine()==1)
printf("%d %d\n", n, unsolved_var[i]);
numInBox(unsolved_var[i], n);//ERROR OCCURS HERE
}
}
}
int numInBox(int space, int n)//space is always three digits long
{
printf("this should print but doesnt(segmentation fault)");
char unsolved_str[3];
sprintf(unsolved_str, "%d", space);//converts int into char[]
printf("%s",unsolved_str);//confirms
int check[3] = {atoi(unsolved_str[0]), 0, atoi(unsolved_str[2])};//converts char[] to int[]
完整的代码......它很长,你运行良好。如果你输入解决那么生病就会抛出那个错误。
`#include <stdio.h>
#include <string.h>
char grid[20][42];
void display()
{
int j;
int u;
for (j = 0; j <= 19; j++)
{
for (u = 0; u <= 39; u++)
{
printf("%c",grid[j][u]);
}
printf("\n");
}
printf("\n");
}
int* unsolved()
{//2-18 4,8-36
int r;
int c;
int size = 0;
static int unsolved_var[81];
unsolved_var[0] = 0;
for (r = 2; r <= 18; r+=2)
{
for (c = 4; c <=36; c+=4)
{
if (grid[r][c] == ' ')
{
if (c <= 9)
{
unsolved_var[size] = r/2*100 + c/4;
size++;
unsolved_var[size] = 0;//sets end of array
}
else
{
unsolved_var[size] = r/2*100 + c/4;
size++;
unsolved_var[size] = 0;
}
// 100 1000 100 1000
//2-0-4 2-0-10 10-0-4 10-0-10
}
}
}
int p;
//for (p = 0; p <= size; p++){printf("%d", unsolved_var[p]);}
return unsolved_var;
}
int numInBox(int space, int n)
{
char unsolved_str[3];
sprintf(unsolved_str, "%d", space);
printf("%s",unsolved_str);
int check[3] = {atoi(unsolved_str[0]), 0, atoi(unsolved_str[2])};
}//***********************************************************************
static int x = 0;
int solve(int* unsolved_var)
{
if (unsolved_var[0] == 0 || x == 10){return 0;}
else
{
int i;
for (i = 0; unsolved_var[i] != 0; i++)
{
int n;
for (n = 1; n <= 9; n++)
{
//if (!numInBox(unsolved_var[i], n)==1 || !numInLine()==1)
printf("%d %d\n", n, unsolved_var[i]);//
numInBox(unsolved_var[i], n);
}
}
x++;
}
}
main()
{
//*****************************GRID******************************
//2 4 6 8 10 12 14 16 18
//3-5 7-9 11-13 15-17 19-21 23-25 27-29 31-33 35-37
char top[] = "\t 1 2 3 4 5 6 7 8 9 ";
char line0[] = "\t+---+---+---+---+---+---+---+---+---+";
char field1[]="1\t| | | | | | | | | |";
char line1[] = "\t+---+---+---+---+---+---+---+---+---+";
char field2[]="2\t| | | | | | | | | |";
char line2[] = "\t+---+---+---+---+---+---+---+---+---+";
char field3[]="3\t| | | | | | | | | |";
char line3[] = "\t+---+---+---+---+---+---+---+---+---+";
char field4[]="4\t| | | | | | | | | |";
char line4[] = "\t+---+---+---+---+---+---+---+---+---+";
char field5[]="5\t| | | | | | | | | |";
char line5[] = "\t+---+---+---+---+---+---+---+---+---+";
char field6[]="6\t| | | | | | | | | |";
char line6[] = "\t+---+---+---+---+---+---+---+---+---+";
char field7[]="7\t| | | | | | | | | |";
char line7[] = "\t+---+---+---+---+---+---+---+---+---+";
char field8[]="8\t| | | | | | | | | |";
char line8[] = "\t+---+---+---+---+---+---+---+---+---+";
char field9[]="9\t| | | | | | | | | |";
char line9[] = "\t+---+---+---+---+---+---+---+---+---+";
int o;
for (o = 0; o <= 39; o++)
{
grid[0][o] = top[o];
grid[1][o] = line0[o];
grid[2][o] = field1[o];
grid[3][o] = line1[o];
grid[4][o] = field2[o];
grid[5][o] = line2[o];
grid[6][o] = field3[o];
grid[7][o] = line3[o];
grid[8][o] = field4[o];
grid[9][o] = line4[o];
grid[10][o] = field5[o];
grid[11][o] = line5[o];
grid[12][o] = field6[o];
grid[13][o] = line6[o];
grid[14][o] = field7[o];
grid[15][o] = line7[o];
grid[16][o] = field8[o];
grid[17][o] = line8[o];
grid[18][o] = field9[o];
grid[19][o] = line9[o];
}
display();
//****************************INPUT******************************
while(1)
{
char input_raw[7];
printf("Enter a Command\n");
fgets(input_raw, 256, stdin);
input_raw[strlen(input_raw)-1] = '\0';
if (strcmp(input_raw, "solve") == 0){solve(unsolved());}
else
{
char *first = input_raw;
char *second = input_raw;
char *third = input_raw;
char *copy = input_raw;
int flag = 1;
while (*copy)
{
if (*copy == ',')
{
*copy = '\0';
if (flag==0) {third = ++copy;}
if (flag==1) {second = ++copy; flag = 0;}
}
else copy++;
}
switch (first[0])
{
case '1':
{
switch (second[0])
{
case '1':{grid[2][3] = '*'; grid[2][4]=third[0]; grid[2][5] = '*'; break;}
case '2':{grid[2][7] = '*'; grid[2][8]=third[0]; grid[2][9] = '*'; break;}
case '3':{grid[2][11] = '*';grid[2][12]=third[0];grid[2][13] = '*';break;}
case '4':{grid[2][15] = '*';grid[2][16]=third[0];grid[2][17] = '*';break;}
case '5':{grid[2][19] = '*';grid[2][20]=third[0];grid[2][21] = '*';break;}
case '6':{grid[2][23] = '*';grid[2][24]=third[0];grid[2][25] = '*';break;}
case '7':{grid[2][27] = '*';grid[2][28]=third[0];grid[2][29] = '*';break;}
case '8':{grid[2][31] = '*';grid[2][32]=third[0];grid[2][33] = '*';break;}
case '9':{grid[2][35] = '*';grid[2][36]=third[0];grid[2][37] = '*';break;}
}
break;
}
case '2':
{
switch (second[0])
{
case '1':{grid[4][3] = '*'; grid[4][4]=third[0]; grid[4][5] = '*'; break;}
case '2':{grid[4][7] = '*'; grid[4][8]=third[0]; grid[4][9] = '*'; break;}
case '3':{grid[4][11] = '*';grid[4][12]=third[0];grid[4][13] = '*';break;}
case '4':{grid[4][15] = '*';grid[4][16]=third[0];grid[4][17] = '*';break;}
case '5':{grid[4][19] = '*';grid[4][20]=third[0];grid[4][21] = '*';break;}
case '6':{grid[4][23] = '*';grid[4][24]=third[0];grid[4][25] = '*';break;}
case '7':{grid[4][27] = '*';grid[4][28]=third[0];grid[4][29] = '*';break;}
case '8':{grid[4][31] = '*';grid[4][32]=third[0];grid[4][33] = '*';break;}
case '9':{grid[4][35] = '*';grid[4][36]=third[0];grid[4][37] = '*';break;}
}
break;
}
case '3':
{
switch (second[0])
{
case '1':{grid[6][3] = '*'; grid[6][4]=third[0]; grid[6][5] = '*'; break;}
case '2':{grid[6][7] = '*'; grid[6][8]=third[0]; grid[6][9] = '*'; break;}
case '3':{grid[6][11] = '*';grid[6][12]=third[0];grid[6][13] = '*';break;}
case '4':{grid[6][15] = '*';grid[6][16]=third[0];grid[6][17] = '*';break;}
case '5':{grid[6][19] = '*';grid[6][20]=third[0];grid[6][21] = '*';break;}
case '6':{grid[6][23] = '*';grid[6][24]=third[0];grid[6][25] = '*';break;}
case '7':{grid[6][27] = '*';grid[6][28]=third[0];grid[6][29] = '*';break;}
case '8':{grid[6][31] = '*';grid[6][32]=third[0];grid[6][33] = '*';break;}
case '9':{grid[6][35] = '*';grid[6][36]=third[0];grid[6][37] = '*';break;}
}
break;
}
case '4':
{
switch (second[0])
{
case '1':{grid[8][3] = '*'; grid[8][4]=third[0]; grid[8][5] = '*'; break;}
case '2':{grid[8][7] = '*'; grid[8][8]=third[0]; grid[8][9] = '*'; break;}
case '3':{grid[8][11] = '*';grid[8][12]=third[0];grid[8][13] = '*';break;}
case '4':{grid[8][15] = '*';grid[8][16]=third[0];grid[8][17] = '*';break;}
case '5':{grid[8][19] = '*';grid[8][20]=third[0];grid[8][21] = '*';break;}
case '6':{grid[8][23] = '*';grid[8][24]=third[0];grid[8][25] = '*';break;}
case '7':{grid[8][27] = '*';grid[8][28]=third[0];grid[8][29] = '*';break;}
case '8':{grid[8][31] = '*';grid[8][32]=third[0];grid[8][33] = '*';break;}
case '9':{grid[8][35] = '*';grid[8][36]=third[0];grid[8][37] = '*';break;}
}
break;
}
case '5':
{
switch (second[0])
{
case '1':{grid[10][3] = '*'; grid[10][4]=third[0]; grid[10][5] = '*'; break;}
case '2':{grid[10][7] = '*'; grid[10][8]=third[0]; grid[10][9] = '*'; break;}
case '3':{grid[10][11] = '*';grid[10][12]=third[0];grid[10][13] = '*';break;}
case '4':{grid[10][15] = '*';grid[10][16]=third[0];grid[10][17] = '*';break;}
case '5':{grid[10][19] = '*';grid[10][20]=third[0];grid[10][21] = '*';break;}
case '6':{grid[10][23] = '*';grid[10][24]=third[0];grid[10][25] = '*';break;}
case '7':{grid[10][27] = '*';grid[10][28]=third[0];grid[10][29] = '*';break;}
case '8':{grid[10][31] = '*';grid[10][32]=third[0];grid[10][33] = '*';break;}
case '9':{grid[10][35] = '*';grid[10][36]=third[0];grid[10][37] = '*';break;}
}
break;
}
case '6':
{
switch (second[0])
{
case '1':{grid[12][3] = '*'; grid[12][4]=third[0]; grid[12][5] = '*'; break;}
case '2':{grid[12][7] = '*'; grid[12][8]=third[0]; grid[12][9] = '*'; break;}
case '3':{grid[12][11] = '*';grid[12][12]=third[0];grid[12][13] = '*';break;}
case '4':{grid[12][15] = '*';grid[12][16]=third[0];grid[12][17] = '*';break;}
case '5':{grid[12][19] = '*';grid[12][20]=third[0];grid[12][21] = '*';break;}
case '6':{grid[12][23] = '*';grid[12][24]=third[0];grid[12][25] = '*';break;}
case '7':{grid[12][27] = '*';grid[12][28]=third[0];grid[12][29] = '*';break;}
case '8':{grid[12][31] = '*';grid[12][32]=third[0];grid[12][33] = '*';break;}
case '9':{grid[12][35] = '*';grid[12][36]=third[0];grid[12][37] = '*';break;}
}
break;
}
case '7':
{
switch (second[0])
{
case '1':{grid[14][3] = '*'; grid[14][4]=third[0]; grid[14][5] = '*'; break;}
case '2':{grid[14][7] = '*'; grid[14][8]=third[0]; grid[14][9] = '*'; break;}
case '3':{grid[14][11] = '*';grid[14][12]=third[0];grid[14][13] = '*';break;}
case '4':{grid[14][15] = '*';grid[14][16]=third[0];grid[14][17] = '*';break;}
case '5':{grid[14][19] = '*';grid[14][20]=third[0];grid[14][21] = '*';break;}
case '6':{grid[14][23] = '*';grid[14][24]=third[0];grid[14][25] = '*';break;}
case '7':{grid[14][27] = '*';grid[14][28]=third[0];grid[14][29] = '*';break;}
case '8':{grid[14][31] = '*';grid[14][32]=third[0];grid[14][33] = '*';break;}
case '9':{grid[14][35] = '*';grid[14][36]=third[0];grid[14][37] = '*';break;}
}
break;
}
case '8':
{
switch (second[0])
{
case '1':{grid[16][3] = '*'; grid[16][4]=third[0]; grid[16][5] = '*'; break;}
case '2':{grid[16][7] = '*'; grid[16][8]=third[0]; grid[16][9] = '*'; break;}
case '3':{grid[16][11] = '*';grid[16][12]=third[0];grid[16][13] = '*';break;}
case '4':{grid[16][15] = '*';grid[16][16]=third[0];grid[16][17] = '*';break;}
case '5':{grid[16][19] = '*';grid[16][20]=third[0];grid[16][21] = '*';break;}
case '6':{grid[16][23] = '*';grid[16][24]=third[0];grid[16][25] = '*';break;}
case '7':{grid[16][27] = '*';grid[16][28]=third[0];grid[16][29] = '*';break;}
case '8':{grid[16][31] = '*';grid[16][32]=third[0];grid[16][33] = '*';break;}
case '9':{grid[16][35] = '*';grid[16][36]=third[0];grid[16][37] = '*';break;}
}
break;
}
case '9':
{
switch (second[0])
{
case '1':{grid[18][3] = '*'; grid[18][4]=third[0]; grid[18][5] = '*'; break;}
case '2':{grid[18][7] = '*'; grid[18][8]=third[0]; grid[18][9] = '*'; break;}
case '3':{grid[18][11] = '*';grid[18][12]=third[0];grid[18][13] = '*';break;}
case '4':{grid[18][15] = '*';grid[18][16]=third[0];grid[18][17] = '*';break;}
case '5':{grid[18][19] = '*';grid[18][20]=third[0];grid[18][21] = '*';break;}
case '6':{grid[18][23] = '*';grid[18][24]=third[0];grid[18][25] = '*';break;}
case '7':{grid[18][27] = '*';grid[18][28]=third[0];grid[18][29] = '*';break;}
case '8':{grid[18][31] = '*';grid[18][32]=third[0];grid[18][33] = '*';break;}
case '9':{grid[18][35] = '*';grid[18][36]=third[0];grid[18][37] = '*';break;}
}
break;
}
}
}
display();
}
}
答案 0 :(得分:3)
numInBox
中存在(至少)两个问题首先,unsolved_str
没有空间终止nul,所以你在堆栈上乱涂乱画。第二个atoi
需要char*
,但您要给它一个char
参数。 (应该至少得到一个警告。)这几乎肯定会在atoi
内产生分段错误。
答案 1 :(得分:1)
我们无法看到整个代码,但我认为您已定义char unsolved_str[3];
并且可能已将其传递给int solve(int* unsolved_var)
。所以你已经将char数组作为int *
参数传递。
同样在solve
循环的for
中,您扫描数组时找不到0
。如果数组没有0,那么循环将超出数组范围。因此,也要包括数组长度,作为for
循环中边界条件的测试。
再次在内部for
循环中,您已使用i=1
迭代循环到9
。请检查数组边界,如果在传递的数组中有足够的分配空间来访问元素索引9
。
答案 2 :(得分:1)
您必须为字符串null终止符留出空间。使用4个字符,而不是3个字符。
答案 3 :(得分:1)
由于printf()
缓冲,因此将其用作指示您的分段错误究竟出现在哪条线上通常不是一个好主意。一般来说,对stderr
的写入不像stdout
那样缓冲,所以如果你想看看你在崩溃前走了多远,你应该做更像{{1}的事情。 }}
话虽如此,你的真正问题在于
fprintf(stderr, "Got Here before crash...\n");
如果查看编译器警告,您应该看到类似的内容:
int check[3] = {atoi(unsolved_str[0]), 0, atoi(unsolved_str[2])};
您需要重新编写该行,以便将常量指针传递给warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast
/usr/include/stdlib.h:148: note: expected ‘const char *’ but argument is of type ‘char’
到char
,而不是atoi()
类型的对象。因此,通过添加地址运算符来尝试此操作:
char
现在将传递int check[3] = {atoi(&unsolved_str[0]), 0, atoi(&unsolved_str[2])};
期待的正确指针类型。
答案 4 :(得分:0)
这部分令人担忧:
char unsolved_str[3];
sprintf(unsolved_str, "%d", space);//converts int into char[]
unsolved_str
如果space
确实是三位数,则{{1}}中没有空余的空终止符。你需要第四个角色的空间。