how can I access specific indexes through pointers?
Depends on the pointer. You can declare a pointer to an array of 3 ints, that way you can just access the pointer almost as-if the array (but watch out for sizeof):
int (*ptr)[3] = nums;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
printf("[%d][%d] = %d\n", i, j, ptr[i][j]);
// since you mentioned `a[b] == *(a + b)` some alternatives:
// or printf("[%d][%d] = %d\n", i, j, *(*(ptr + i) + j));
// or printf("[%d][%d] = %d\n", i, j, *(ptr[i] + j));
// or printf("[%d][%d] = %d\n", i, j, (*(ptr + i))[j]);
// or pls dont printf("[%d][%d] = %d\n", i, j, i[ptr][j]);
}
}
You use a pointer to the array of 2 arrays of 3 ints. Semantically, it's a different thing and you have to first dereference the pointer:
int (*var)[2][3] = &nums;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
printf("[%d][%d] = %d\n", i, j, (*var)[i][j]);
}
}
Because elements in an array are laid in memory continuously, you can alias the array with a pointer to int and do the row/column arithmetic yourself:
int *p = (int*)nums;
for (int i = 0; i < 2 * 3; ++i) {
printf("[%d] = %d\n", i, p[i]);
}
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
printf("[%d][%d] = %d\n", i, j, p[i * 3 + j]);
}
}
int *numsptr = &nums;doesn't change anything.int**pointer, notint*which is just 1D.