# Process returned -1073741819 (0xC0000005) in Code Blocks

1

I have been trying to solve polynomial addition with any variable in c so I get a bug when splitting the string and assigning it to an array after assigning my code crashes with the output

``````Enter the number of equations :  1
Enter the 1 Equation :  6x^2+3y^2+3x^1
Substring 6x
Value 6x
i : 0 j : 0 k : 0
Substring 2
Value 2
i : 0 j : 0 k : 1
Substring 3y
Value 3y
i : 0 j : 1 k : 0
Substring 2
Value 2
i : 0 j : 1 k : 1
Substring 3x
Value 3x
i : 0 j : 2 k : 0
Substring 1
Value 1
i : 0 j : 2 k : 1

Process returned -1073741819 (0xC0000005)   execution time : 3.963 s
Press any key to continue.
``````

My code is

``````int no_of_equation;
printf("Enter the number of equations :  ");
scanf("%d",&no_of_equation);
char equation_arr[no_of_equation][59];
for (int i = 0;i < no_of_equation; i++) {
printf("Enter the %d Equation :  ",i+1);
scanf("%s",equation_arr[i]);
}
char *substr;
char coefficient_array[no_of_equation][10][2][6];
for (int i = 0;i < no_of_equation; i++) {
substr = strtok(equation_arr[i],"^+");
while(substr != NULL) {
for(int j = 0;j < 10; j++) {
for(int k = 0;k < 2;k++){
strcpy(coefficient_array[i][j][k],substr);
printf("Substring %s\n",substr);
printf("Value %s\n",coefficient_array[i][j][k]);
printf("i : %d j : %d k : %d\n",i,j,k);
substr = strtok(NULL,"^+");
}
}
}
}
``````
arrays
c
string
pointers
asked on Stack Overflow Nov 6, 2020 by Gowtham T G • edited Nov 6, 2020 by Jem Tucker

1
``````for (int k = 0; k < 2; k++) {
strcpy(coefficient_array[i][j][k], substr);      // substr may be NULL here
printf("Substring %s\n", substr);
printf("Value %s\n", coefficient_array[i][j][k]);
printf("i : %d j : %d k : %d\n", i, j, k);
substr = strtok(NULL, "^+");                     // substr may become NULL here
}
``````

See comments above: the problem happens when you ar at the end of the string, `substr` becomes NULL, and in the next iteration you try to strcpy from a NULL pointer which crashes your program.

You need to put another test into the end condition of the for loop:

``````for (int k = 0; k < 2 && substr != NULL; k++)
``````

I suggest you learn how to use your debugger. With a debugger this kind of simple bugs can be found in minutes.

answered on Stack Overflow Nov 6, 2020 by Jabberwocky
0

There is a strong issue in your program, `strtok` may return `NULL`. And `strcpy` crashes if you put null in parameter.

``````int main ()
{
int no_of_equation;
printf("Enter the number of equations :  ");
scanf("%d",&no_of_equation);
char equation_arr[no_of_equation][59];
for (int i = 0;i < no_of_equation; i++) {
printf("Enter the %d Equation :  ",i+1);
scanf("%s",equation_arr[i]);
}
char *substr;
char coefficient_array[no_of_equation][10][2][6];
for (int i = 0;i < no_of_equation; i++) {
substr = strtok(equation_arr[i],"^+");
while(substr != NULL) {
for(int j = 0;j < 10; j++) {
for(int k = 0;k < 2;k++){
if (substr)
strcpy(coefficient_array[i][j][k],substr);
else
coefficient_array[i][j][k][0] = 0;
printf("Substring %s\n",substr);
printf("Value %s\n",coefficient_array[i][j][k]);
printf("i : %d j : %d k : %d\n",i,j,k);
substr = strtok(NULL,"^+");
}
}
}
}
}
``````
answered on Stack Overflow Nov 6, 2020 by Robert

User contributions licensed under CC BY-SA 3.0