调用方法失败:c 中的分段错误
这段代码是一个大型源代码的一部分,但错误就在这里。
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[]
完整的代码...它很长,你运行得很好。如果您输入solve,那么我会抛出该错误。
`#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();
}
}
This piece of code is part of a large source code but the bug is here.
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[]
Complete Code...its long it runs fine thou. If you type in solve then ill throw you that error.
`#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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
numInBox
中有(至少)两个问题,首先,unsolved_str
没有空间用于 nul 终止,因此您在堆栈上乱写乱画。第二个atoi
接受一个char*
,但您给它一个char
参数。 (至少应该得到一个关于这一点的警告。)这几乎肯定会在atoi
内部产生分段错误。There's (at least) two problems in
numInBox
First,unsolved_str
doesn't have room for the nul termination, so you are scribbling on the stack. Secondatoi
takes achar*
, but you are giving it achar
argument. (Should have gotten at least a warning about that.) That will almost certainly give a segmentation fault insideatoi
.由于 printf() 会缓冲,因此将其用作分段错误发生在哪一行的指示器通常不是一个好主意。一般来说,对
stderr
的写入不会像对stdout
那样进行缓冲,因此如果您想了解崩溃前已经走了多远,您应该这样做更像是fprintf(stderr, "Got Here before crash...\n"); 话
虽这么说,你真正的问题在于
如果你查看编译器警告,你应该看到类似于:
你需要重写该行这样您就可以将指向
char
的常量指针传递给atoi()
,而不是char
类型的对象。因此,请尝试添加一个地址运算符:现在将传递
atoi
所需的正确指针类型。Since
printf()
buffers, using it as an indicator of exactly what line your segmentation fault occurs on is not generally a good idea. In general, writes tostderr
don't buffer like they do tostdout
, so if you are looking to see how far you've gotten before a crash, you should do something more likefprintf(stderr, "Got Here before crash...\n");
That being said, your real problem is in the line
If you look at your compiler warnings, you should be seeing something like:
You need to re-write that line so that you're passing constant pointers to
char
toatoi()
, not objects of typechar
. So try this by adding an address-of operator:That will now pass the proper pointer type that
atoi
is expecting.我们看不到完整的代码,但我认为您已经定义了
char unsolved_str[3];
并且可能已将其传递给intsolve(int* unsolved_var)
。因此,您已将 char 数组作为int *
参数传递。同样,在
for
循环的solve
中,您扫描数组,但未找到0
。如果数组中没有0,则循环将超出数组范围。因此,还包括数组长度,作为for
循环中边界条件的测试。再次在内部
for
循环中,您已将循环从i=1
迭代到9
。请检查数组边界,以及传递的数组中是否有足够的分配空间来访问元素索引9
。We cannot see the entire code but i think that you have defined
char unsolved_str[3];
and probably have passed it to theint solve(int* unsolved_var)
. So you have passed a char array as anint *
argument.Also in the
solve
in thefor
loop you scan the array while a0
is not found. If the array does not have a 0, then the loop will go beyond the array range. So include the array length also, as a test of boundary condition in thefor
loop.Again in the inside
for
loop you have iterated the loop withi=1
to9
. Please check the array boundaries, and if you have enough allocated space in the passed array to access the element index9
.您必须为字符串空终止符留出空间。使用 4 个字符,而不是 3 个。
You must leave room for the string null terminator. Use 4 characters, not 3.
这部分令人担忧:
如果
space
确实是三位数字,则unsolved_str
中没有 NULL 终止符的空间。你需要空间容纳第四个角色。This part is worrying:
There's no space for a NULL terminator in
unsolved_str
ifspace
is indeed three digits. You need room for a fourth character.