Threading and pointers to structures
Hi, I can't get the values in my structure to come out it just seems to print the location in memory. Any help would be much appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
// lab5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <process.h>
using namespace std;
struct ThreadArgs
{
int id;
int delay;
};
unsigned __stdcall MyThread(void *data)
{
ThreadArgs *args = (ThreadArgs *) data;
cout << (int) data << endl;
Sleep(3000);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
ThreadArgs args;
args.id = 4;
args.delay = 10;
HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
WaitForSingleObject(hThread, INFINITE);
return 0;
}
|
cout << (int) data << endl;
You're casting a void pointer to an integer. What did you expect besides the memory address?
Try this on for size:
20 21
|
cout << args->id << endl;
cout << args->delay << endl;
|
Thanks man. I was trying args.id. thanks again
Topic archived. No new replies allowed.