htirwin, this is what I get when I run your program |
The error messages tell you where the errors are. On line 8, I left out the type of the parameter. Should be
int n
( fixed ).
I've been doing a lot of javascript programming lately.
I also had another error on line 25 and more errors in the print statements, and lines 80 to 83. Sorry about that. I was rushing too much. I corrected it.
_beginthread()? I know the first parameter is function name... etc. Thanks |
Here is the signature.
1 2 3 4 5
|
uintptr_t _beginthread(
void( __cdecl *start_address )( void * ),
unsigned stack_size,
void *arglist
);
|
The first parameter denotes a function pointer which is essentially what the function name stands for behind the curtains. It is a pointer to a function that takes a void pointer as an argument and returns nothing ( hence void ). This is why your assist function must have the signature
void assist( void * arg_name )
http://www.learncpp.com/cpp-tutorial/78-function-pointers/
The second is the stack size for the thread.
http://stackoverflow.com/questions/7538008/what-does-beginthreads-second-argument-stack-size-mean
The third parameter is a void pointer
http://www.learncpp.com/cpp-tutorial/613-void-pointers/.
For simplicity, you can think of variables as mailboxes. They have addresses and contents. The address is the pointer, the contents is the value.
You declare pointers with a type to give the compiler some information about what is actually at that address ( the type of the value stored in that address ). This is important, because when you retrieve the value starting at the address, you need to know how much to take and how to interpret it.
When you cast to a void pointer, you strip away the type information and it becomes just the address without any information about the type of value it points to. In C, it is common in functions such as _beginthread to make the parameter a void pointer so that you can use the same function and still pass any kind of data.
It's important that in the function, you give the pointer it's original type back by casting.
Pointer syntax is what confuses a lot of people.
http://www.cplusplus.com/doc/tutorial/pointers/
In my example, I use a struct to let me group the variables in need to pass to the function into one variable. If you have a variable that represents a struct or class by value, then you access its member variables like this.
some_struct.start
if you have a variables which is a pointer to a class or struct, you access it's variables like this,
some_struct->start
If you have a variable representing the value of something, you get the address where that value is in memory like this,
&some_var
.
(void*) &some_struct
, means cast the address of the value held by some_struct to a typeless pointer.
Then in the function, when you use _arg, you have to make sure it is casted to the correct type. You can just put the cast everywhere it is used, or make a new variable of the correct pointer type and set it equal to the void pointer casted to the correct pointer type.
1 2 3 4
|
void( void * _arg ) {
Some_Struct *arg = (Some_Struct*) _arg;
...
}
|