0

I am trying to pass a 2d array into a pthread function, but I can not find a way to get access to the array content during pthread process, how can I do it? I tried

int ** array = (int **)arg;

but it caused segfault after I tried to change the stored value; Here is part of my code:

int message1[2][64];
int i = 0;
for (; i < 2; i++)
{
    int j = 0;
    for (; j < 64; j++)
    {
        message[i][j] = 1;
    }
}
pthread_t tid[1];
pthread_create(&tid[0], NULL, xD, message);

the function:

void * xD(void * arg)
{
     int ** array = (int **)arg;
     array[0][0] = 2;
}

1 Answers1

4

Couldn't find a proper duplicate for this. int ** is not a 2D array but a pointer to a pointer to an int.

What you want to pass in is a pointer to an array[64] of int, i.e. int (*array)[64].

Try

int (*array)[64] = arg;
  • some "array/pointers FAQ" desperately needed ;) Maybe I'll write one some day ... anyways, good answer, to the point in this case! –  Apr 24 '18 at 11:14