#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?

Dani AI

Generated

Quick diagnosis: the snippet from has several independent problems — a bad cast/syntax in the thread routine, the wrong printf format (printing an int with %s), and an unsafe/thread-unsafe approach to lifetime and synchronization. was right to suggest heap allocation, but you also need to cast and dereference correctly, avoid the busy-wait loop in main, and ensure memory is freed exactly once.

A simple, safe pattern is: allocate the struct on the heap, pass the pointer to the thread, wait for the thread to finish, then free (or let the thread free it if ownership is transferred). Example:

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

typedef struct { const char *name; int age; } person_t;

DWORD WINAPI worker(LPVOID arg) {
    person_t *p = (person_t*)arg;
    printf("thread: name=%s age=%d\n", p->name, p->age);
    free(p);            /* if this thread owns the memory */
    return 0;
}

int main(void) {
    person_t *p = malloc(sizeof *p);
    p->name = "John Smith";
    p->age = 25;
    HANDLE h = CreateThread(NULL, 0, worker, p, 0, NULL);
    WaitForSingleObject(h, INFINITE);
    CloseHandle(h);
    return 0;
}

Notes and troubleshooting tips:

  • Cast LPVOID back to struct person * and use -> to access fields; do not try to assign a pointer to a struct value without the correct cast.
  • Use %d for integers, %s for strings. Compiler warnings will catch these mismatches.
  • Avoid while(1) busy loops; WaitForSingleObject or proper synchronization is cleaner and avoids CPU spin.
  • Decide ownership: either free the heap block in the thread or in the creator after joining; do not double-free.
  • If you call many C runtime functions in threads, prefer _beginthreadex on older MSVC runtimes. Enable compiler warnings (-Wall) to catch the syntax/format errors early.

Recommended Answers

All 3 Replies

#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.