I'm trying to iterate through a double pointer without using Array notation. I've been given these guidelines through my instructor.
In .h file:
int** A, B, C;
In .c file
void initNumbers() {
int** a = A;
int** b = B;
int** c = C;
int* p;// = *a;
int* q;// = *b;
int* r;// = *c;
for (int i = 0; i < 10; i++) {
p = *a;
q = *b;
r = *c;
for (int j = 0; j < 10; j++) {
*r = rand() % 50;
*p = rand() % 50;
*q = rand() % 50;
p++;
q++;
r++;
}
a++;
b++;
c++;
}
}
I've allocated memory via.
void initNumSpace() {
A = malloc(sizeof(int*) * 10);
B = malloc(sizeof(int*) * 10);
C = malloc(sizeof(int*) * 10);
int** a = A;
int** b = B;
int** c = C;
for (int i = 0; i < 10; i++) {
a = malloc(sizeof(int) * 10);
b = malloc(sizeof(int) * 10);
c = malloc(sizeof(int) * 10);
a++;
b++;
c++;
}
}
The code gets no syntax errors, but there is a segmentation fault at the very first iteration of the imbedded loop of initNumbers. Why is this happening? What is the best way to iterate through a pointer array without array notation e.g., indices or offset values.