<PSA>First please use
https://gist.github.com/ and then paste the link back in your message, rather than upload the .c and .h. Files. With the latter we can just click a link and see the code. With uploads we have to download, open in editor, examine and finally remember to delete the files. All in all its much more convenient for people you are asking to help to just use a gist. </PSA>
Roland gave you the answer. I'm just going to give you some more detail since you don't seem to understand the "why" yet.
The full posted code declares a global pointer to an anonymous struct.
At this point Command_Packet points to some random location of memory. Later the code does:
Code: Select all
Command_Packet =(SB_OEM_PKT*) malloc(sizeof(SB_OEM_PKT));
This is asking the heap to allocate a chunk of uninitialized memory and return the pointer to that chunk. The cast converts it from void* to to a pointer to your struct. The pointer is assigned to Command_Packet. Command_Packet still doesn't contain anything meaningful, but it does point to a chunk of memory you own. The original code then starts to assign values to the allocated chunk of memory
Code: Select all
/*********Change Baudrate Command***********/
Command_Packet->Head1 = COMMAND_START_CODE1;
Command_Packet->Head2 = COMMAND_START_CODE2;
Command_Packet->wDevId0 = DEVICE_ID;
...
I did remove the "*" from "SB_OEM_PKT* Command_Packet;"
and then switch all the -> to " . " and it worked fine.
When you removed the "*" you said Command_Packet is a structure (instead of being a pointer to a structure, it is now a solid object). So now you can't use -> because that first dereferences a pointer and then accesses the struct member. Instead you just use dot operator "." which accesses the member.
In the code you posted originally, you left off the call to malloc that returns a pointer. Instead you declared a pointer to a struct but never assigned that pointer to anything. Worse than trying to print it, (much worse), is if you did something like Command_Packet->Head1 = anything; you are overwriting some random memory location. If other code is using that memory, when you read that location back it may or may not have the value you wrote there. At any rate it's a good way to crash your app.
You can free memory that has been allocated from the heap with malloc. When you make it a global solid object it takes its memory from the global variable space and that memory can't be freed. That' not necessarily a bad thing if the struct is going to live for the lifetime of the app. And if it's not going to live for the lifetime of the app I wouldn't allow it to be a global variable. Well, I wouldn't allow it to be a global variable no matter what but that's a different lecture.
Earlier this year I made a series of screencasts on programming in C++ for a class I was teaching. This included four
lessons on pointers. The whole playlist of lessons should show on the right, so if you find the first one helpful you should be able to click and watch all four.