2

I have a structure defined in C as

typedef struct shape{
   int height;
   int width;

} rectangle;

// just to denote the rectangles where width > height
typedef rectangle wider;

wider all[3]={{5,10},{2,4},{7,9}};

and I have a function that prints the height and width

void funct(wider shape){
printf("height: %d, width %d", shape.height, shape.width);
}

Now I want to achieve this for each shape, by creating threads and so I tried this:

pthread_t threads[sizeof(all)];
int count=0;
for(count=0; count <sizeof(all);++count)
{
  if(pthread_create(&threads[count], NULL, funct,(wider*)all[count])!=0)
    {
    printf("Error in creating thread: %d",count);
   break;
    }
}

int i=0;
for(i=0; i<count; ++i)
{
if(pthread_join(threads[i],NULL)!=0)
  printf ("Eroor joining: %d"+i);
}

However, this shows an error

expected 'void * (*)(void *)' but argument is of type 'void (*)(struct rectangle)'

I tried changing my function to

void funct(void *wide){
wider shape=(wider)wide;
// print same as before
}

but this still doesn't work. what am I doing wrong?

P.P
  • 117,907
  • 20
  • 175
  • 238
CoderBC
  • 1,262
  • 2
  • 13
  • 30
  • `wider shape = *(wider*)wide;`, pass `&all[count]` instead of `(wider*)all[count]`. Pointers. – Ry- Oct 26 '16 at 17:45
  • 1
    Read the error message again. Is the argument to the thread function *a pointer*? Does it *return* a pointer? – Some programmer dude Oct 26 '16 at 17:47
  • 1
    Also, the `sizeof` operator returns the size of its operands *in bytes*. For your `sizeof(all)` it will return `sizeof(wider) * 3` bytes. Not the number of elements in the array. That will affect your `threads` array size. – Some programmer dude Oct 26 '16 at 17:48
  • Lastly, a pointer is not a structure, and a structure is not a pointer. Pointers and structures are not the same and are you can't just cast them to one another. [Find a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and reread the chaper(s) on pointers, and especially about the address-of operator `&`. – Some programmer dude Oct 26 '16 at 17:49

1 Answers1

2

pthread_create() expects a function argument that takes a void* as input and returns void*. But your function does neither. So, the compiler complains about the type mismatch.

Instead change the function:

void* funct(void *arg){
   wider shape = *(wider*)arg;
   printf("height: %d, width %d", shape.height, shape.width);
   return NULL;
}

Your argument passing has another problem. You are not passing the address of the all[count] but converting a wider into void*. Your sizeof calculation is wrong too. You should divide by sizeof(all[0]) to get the correct number of wider elements in the all array.

pthread_t threads[sizeof(all)/sizeof(all[0])];
int count=0;
for(count=0; count <sizeof(all)/sizeof(all[0]);++count)
{
  if(pthread_create(&threads[count], NULL, funct,&all[count])!=0)
    {
       printf("Error in creating thread: %d",count);
       break;
    }
}
P.P
  • 117,907
  • 20
  • 175
  • 238