Home>

I tried to make only the part of the program that made the city that moved when creating the program to create the circuit and the array to leave the footprint that moved, but the compilation passes. Can't move due to segment error, please give me advice.

Error message
Segment error
Applicable source code

`` `C language

include include

typedef struct {
int * tour;
char * visited;
} a_struct;

a_struct * a;

int main (void)
{
int i, j;
int n;
n = 5;
for (i = 0;i<4;i ++) {
a [i] .tour = calloc (n + 1, sizeof (int));
a [i] .visited = calloc (n, sizeof (char));
}

for (i = 0;i<4;i ++) {
for (j = 0;j a [i] .tour [j] = j;
a [i] .visited [j] = 1;
}
}

for (i = 0;i<4;i ++) {
for (j = 0;j printf ("% d \ n", a [i] .tour [j]);
printf ("% d \ n", a [i] .visited [j]);
}
}
}

An attempt was made to prepare an array without using calloc, but a segment error occurred in the same way, and the situation was full.

c
  • Answer # 1

    a_struct * a;
    // abbreviated
    a [i] .tour = calloc (n + 1, sizeof (int));
    A segment error occurs because the area

    a cannot be secured.
    Secure area a.

  • Answer # 2

      

    a [i] .tour = calloc (n + 1, sizeof (int));

    calloc is not a problem
    The problem is that the array area for a is not reserved