Nerdic Sapo Nerdic Sapo -4 years ago 92
C Question

Segmentation Fault occuring while assigning values of array

I'm trying to set array's length according as a file's line number. I declared my arrays in main function and passed to setMatrix.

main function

int x;
int graph[x][x];
int path[x];
char *pathString[x];

setMatrix function

void setMatrix(int **graph,int *path,char **pathString,int k){
int i,j;
graph[i][j]=INFINITY;//I get error here

I'm sure I did an easy mistake but couldn't see.

Answer Source

The function definition should match the arrays:

void setMatrix(int x, int graph[x][x], int path[x], char *pathString[x])

The int x has to be first so that it is in scope for the later parameters. It is possible to use pointer notation instead of the innermost array bound, but it serves as self-documentation to use the array notation.

Arrays and pointers are different; int ** is not the same as int[x][x].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download