Q:

Using threads with structures

My goal is to create 3 threads, each one with a struct associated with it then let it print its content. 
I have this piece of code:  

int main() {

  pthread_t threads[3];
  int threads_id[3];
  int i;

  typedef struct mythread{
    pthread_t tid;
    char string[];
  };

  for(i =0; i<3; i++) {
    threads_id[i] = i;
    struct mythread = {i, "This is the thread number"};
    pthread_create(&threads[i], NULL, print_struct, &mythread);
  }

  for(i =0; i<3; i++) {
    pthread_join(threads[i], NULL);
  }

  return 0;
}

void* print_struct(void * arg) {
  struct *p1 = (struct*) arg;
  struct p2 = *p1;

  printf("%s", p2.string);
  printf(" ");
  printf("%d", p2.tid);

  pthread_exit(NULL);
}

Buut it is not working, what am I doing wrong?

A:

Your code with just a bit of cleanup.
/**
  gcc -std=c99 -o prog_c prog_c.c -pthread \
      -pedantic -Wall -Wextra -Wconversion \
      -Wc++-compat -Wwrite-strings -Wold-style-definition -Wvla \
      -g -O0 -UNDEBUG -fsanitize=address,undefined
**/

#include <stdio.h>   // for printf()
#include <string.h>  // for strcpy()
#include <pthread.h> // for pthread_t, and related functions

// declare this type _before_ using it
typedef struct
{
  pthread_t tid;   // no need to store this in another array
  int id;          // just count the threads with an easy to read number
  char string[80]; // you forgot to give the size of the storage
} mythread;        // this way, ``mythread'' is a correct name for a type

void *
print_struct(void *arg)
{
  mythread *t=(mythread *)arg; // access a _single_ structure (out of 3)
  printf("%s %d\n", t->string, t->id); // display its properties
  pthread_exit(NULL); // I would have preferred a simple ''return NULL;''
}

int
main(void)
{
  mythread threads[3]; // everything will be stored here
  for(int i=0; i<3; ++i)
  {
    // each thread will use its own structure in the array (1 out of 3)
    threads[i].id=i; // the ``easy to display'' number
    strcpy(threads[i].string,            // provide each thread with its
           "This is the thread number"); // own message (the same here)
    pthread_create(&threads[i].tid, NULL,      // tid is updated
                   print_struct, &threads[i]); // only 1 structure (out of 3) 
  }
  for(int i=0; i<3; ++i)
  {
    pthread_join(threads[i].tid, NULL); // reuse the stored tid
  }
  return 0;
}

