Thread: Memset with 2d arrays

1. Memset with 2d arrays

Hi folks. I am currently confusing myself over why the first code doesn't work when the second code works. Would someone explain to me? The differences in codes have been bolded out.

Code:
int matrix;
int vertexes;

int** matrix = adjMat -> matrix;
matrix[node1][node2] = 1;
matrix[node2][node1] = 1;
}

int v = adjMat->vertexes;
int** mat = adjMat->matrix;
for(int i = 1; i <=v; i++){
printf("Vertex %d is connected to ... ", i);
for(int j = 1; j <= v; j++){
}
}
}

int main()
{

mat.vertexes = 8;
for(int i = 1; i <= mat.vertexes; i ++){
memset(mat.matrix[i], 0x00, sizeof(int)*9);
}
showGraph(&mat);
return 0;
}
Code:
......
int main()
{

mat.vertexes = 8;
for(int i = 1; i <= mat.vertexes; i ++){
memset(mat.matrix[i], 0x00, sizeof(int)*9);
}
showGraph(&mat);
return 0;
}
The only difference is with the argument mat.matrix[i] vs mat.matrix[i] to the memset function.

Also, I realized that when I degrade the 2d array variable matrix into double pointer in the showGraph() function, and then try to access the value from that double pointer, the code does not work.

Code:
int v = adjMat->vertexes;
int** mat = adjMat->matrix;
for(int i = 1; i <=v; i++){
printf("Vertex %d is connected to ... ", i);
for(int j = 1; j <= v; j++){
printf("%d, ", mat[i][j]);
}
}
}
Thanks for helping out! 2. The first one doesn't work because "mat.matrix[i]" isn't a pointer. memset expects a pointer as its first argument. Originally Posted by crystalize123 Also, I realized that when I degrade the 2d array variable matrix into double pointer in the showGraph() function, and then try to access the value from that double pointer, the code does not work.
Of course, since you're trying to assign an "int[][]" to a "double**". The compiler objects heavily when you try to assign pointers of different types (if it doesn't, it should). 3. Originally Posted by GReaper Of course, since you're trying to assign an "int[][]" to a "double**". The compiler objects heavily when you try to assign pointers of different types (if it doesn't, it should).
Oops, it's an typo. I assigned an "int[][]" to a "int**", as shown here with int** mat.

Code:
int v = adjMat->vertexes;
int** mat = adjMat->matrix;
for(int i = 1; i <=v; i++){
printf("Vertex %d is connected to ... ", i);
for(int j = 1; j <= v; j++){
printf("%d, ", mat[i][j]);
}
}
}
Would you tell me why mat[i][j] won't work?
And also what would "mat.matrix[i]" be if it's not a pointer? 4. Originally Posted by crystalize123 Would you tell me why mat[i][j] won't work?
Because you may think that "int[][]" and "int**" are equivalent, but they are not. The compiler lets you use static arrays as pointers only for convenience, and in this case a 2D static array is entirely different compared to a double pointer. Originally Posted by crystalize123 And also what would "mat.matrix[i]" be if it's not a pointer?
What you have declared it as, int. Popular pages Recent additions Tags for this Thread

2d array and pointer, beginner, double pointer, int, memset 