I have the following code that calls the function uint32_pack. This program compiles with no errors in Dev-C++ but does not produce the correct result when ‘tag’ is an even number. In Visual Studio the program compiles but produces errors and I’m guessing that these errors are why I don’t get the correct output when ‘tag’ is even. I’m still trying to get my head around pointers and I’m not sure where I went wrong when declaring them. Thanks for your help.
Here is the code where the errors come from:
1 int uint32_pack (uint8_t *fieldnumber, uint32_t value, uint8_t *out);
2 int main(){
3 uint32_t initvalue = 2;
4 int return_rv;
5 uint8_t *tag = (uint8_t *) malloc(sizeof(uint8_t));
6 *tag = 38;
7 uint8_t *tempout= (uint8_t *) malloc(30);
8 return_rv = uint32_pack (tag, initvalue, tempout);
9 free(tempout);
10 free(tag);
11 }
And the errors from VS as are follows:
error C2143: syntax error : missing ';' before 'type' (on line 7)
error C2065: 'tempout' : undeclared identifier (on line 8)
warning C4047: 'function' : 'unsigned char *' differs in levels of indirection from 'int' (on line 8)
warning C4024: 'uint32_pack' : different types for formal and actual parameter 3 (on line 8)
error C2065: 'tempout' : undeclared identifier (on line 9)
warning C4022: 'free' : pointer mismatch for actual parameter 1 (on line 9)
The last three errors are consequences of the second, and the second is a consequence of the first. That only leaves the first and third unexplained.
The first error occurs because you are using C89 and not C++ or C99; you cannot declare variables after code in C89.
Reverse the order of lines 6 and 7 and you should probably be OK. (I think the third error is also a consequence of the first, too, but that is not definitive.)