Home > Software design >  double pointers w/o array notation
double pointers w/o array notation

Time:09-22

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.

CodePudding user response:

The error is in your init function. The double pointers get set to the newly allocated space, but the pointer array A does not get set to the value of a. Try this instead:

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  ;
}

CodePudding user response:

Your code has a few problems.

First of all, you shouldn't DEFINE variables in header files; you should only DECLARE them. That is, you if you wanted to declare an integer N, then you shouldn't write int N; but instead extern int N; and then go to your source file and write int N; to define it. C (and C ) have the multiple declaration-one definition rule: you can declare something as many times as you like (you can write 'extern int A;five times in a row and the compiler won't mind), but you have to define each variable and function exactly once (you should writeint A;` exactly once in a source file.

Secondly, there's a slight problem with your header file. In C, writing int** A,B,C; is the same as int **A;int B;int C;. A is a double pointer, but B and C are integers. So, you should write int **A,**B,**C; (or even better, write each one on a separate line).

Your code should now look like this:

HEADER:

int** A;
int** B;
int** C;

SOURCE:

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  ) {
        //you should dereference these btw
        *a = malloc(sizeof(int) * 10);
        *b = malloc(sizeof(int) * 10); 
        *c = malloc(sizeof(int) * 10); 

        a  ;
        b  ;
        c  ;
    }
}

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  ;
    }
}

PS: why are you doing this way? It's not wrong, but it's really comlicated and hard to read. Why would you not want to use array indexing ([]).

  • Related