#include <stdio.h>
#include <windows.h>
#include <stdlib.h>

DWORD WINAPI test_thread(LPVOID lpParam)
{
printf("thread whiiiii\n");

struct person
{
   char *namee;
   int agee;
};
struct person p = ((struct person)lpParam;
	printf("%s\n",p.agee);
	ExitThread(0);
}
 
int main()
{
struct person
{
   char *name;
   int age;
};
 DWORD thread;
   struct person p;
   p.name = "John Smith";
   p.age = 25;
   printf("%s\n",p.name);
   printf("%d\n",p.age);
   CreateThread(NULL,0,test_thread,(LPVOID)&p,0,&thread);
   while (1)
   {
   }
   return 0;
}

Some reason it doesn't work, Can anyone help?

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>

DWORD WINAPI test_thread(LPVOID lpParam)
{
printf("thread whiiiii\n");

struct person
{
   char *namee;
   int agee;
};
struct person p = ((struct person)lpParam;
	printf("%s\n",p.agee);
	ExitThread(0);
}
 
int main()
{
struct person
{
   char *name;
   int age;
};
 DWORD thread;
   struct person p;
   p.name = "John Smith";
   p.age = 25;
   printf("%s\n",p.name);
   printf("%d\n",p.age);
   CreateThread(NULL,0,test_thread,(LPVOID)&p,0,&thread);
   while (1)
   {
   }
   return 0;
}

Some reason it doesn't work, Can anyone help?

May be because 'p' is in stack and stack is not shared between threads....
Can you allocate for p[ making p a pointer] dynamically and test this again..

How would i go about doing it dynamically?

How would i go about doing it dynamically?

Use malloc

make it

struct person p*;
p = malloc(sizeof(struct person));
p->name = "John Smith";
p->age = 25;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.