Some help with typedef and pointers.

Discussion to talk about software related topics only.
Post Reply
jdal
Posts: 21
Joined: Sat Jan 07, 2012 7:10 am

Some help with typedef and pointers.

Post by jdal »

I have always had trouble with pointers. Would someone please help me to understand how to print out the value of Head1.
This chunk of example code came from a vender. So no need to ask why i am doing this or that ;)

Code: Select all


#define COMMAND_START_CODE1 	0x55
#define COMMAND_START_CODE2 	0xAA

typedef struct {
	unsigned char 	Head1;
	unsigned char 	Head2;
	unsigned char	wDevId0;
	unsigned char	wDevId1;
	unsigned char	nParam0;
	unsigned char	nParam1;
	unsigned char	nParam2;
	unsigned char	nParam3;
	unsigned char	wCmd0;// or nAck
	unsigned char	wCmd1;
	unsigned char 	wChkSum0;
	unsigned char 	wChkSum1;
} SB_OEM_PKT;


SB_OEM_PKT* Command_Packet;

	/*********Change Baudrate Command***********/
	Command_Packet->Head1 = COMMAND_START_CODE1;
	Command_Packet->Head2 = COMMAND_START_CODE2;
	Command_Packet->wDevId0 = DEVICE_ID;
	Command_Packet->wDevId1 = DEVICE_ID>>8;
	Command_Packet->nParam0 = State & 0xFF;
	Command_Packet->nParam1 = (State & 0xFF00) >> 8 ;
	Command_Packet->nParam2 = (State & 0xFF0000) >> 16;
	Command_Packet->nParam3 = (State & 0xFF000000) >> 24;
	Command_Packet->wCmd0 = CMOSLED;
	Command_Packet->wCmd1 = ZERO;
	tmp = CalcChkSumOfCmdAckPkt(Command_Packet);
	Command_Packet->wChkSum0 = tmp % 256;
	Command_Packet->wChkSum1 = tmp >> 8;
	/*********Change Baudrate Command***********/

        // the result is 0, and i do not know why. 
	iprintf("%x", (unsigned char*)Command_Packet->Head1);

roland.ames

Re: Some help with typedef and pointers.

Post by roland.ames »

your code should be:

Code: Select all

   iprintf("%x", (int)(Command_Packet->Head1));

Command_Packet is a pointer to a structure.

(Command_Packet->Head1) is a unsigned char.

cast it to an int because you have used %x, which expects an int parameter.


I am sure tod will explain how to do this using iostream, and why it is a much better way of doing it!
I am surprised he hasn't done it already. :)

Roland
jdal
Posts: 21
Joined: Sat Jan 07, 2012 7:10 am

Re: Some help with typedef and pointers.

Post by jdal »

Thanks, but still returns 0
roland.ames

Re: Some help with typedef and pointers.

Post by roland.ames »

I cannot see in your code where Command_Packet gets initialised.

It should be initialised to the address of a structure.

something like:

Code: Select all


SB_OEM_PKT pkt_buffer;

Command_Packet = &pkt_buffer;

jdal
Posts: 21
Joined: Sat Jan 07, 2012 7:10 am

Re: Some help with typedef and pointers.

Post by jdal »

In the code sent to me, they did not do this. I did remove the "*" from "SB_OEM_PKT* Command_Packet;"
and then switch all the -> to " . " and it worked fine.
I was more curious why the original did not work. Attached is the original files.
Attachments
Protocol.h
(3.73 KiB) Downloaded 359 times
Protocol.c
(18.68 KiB) Downloaded 360 times
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: Some help with typedef and pointers.

Post by tod »

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

Code: Select all

SB_OEM_PKT* Command_Packet;
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.
jdal
Posts: 21
Joined: Sat Jan 07, 2012 7:10 am

Re: Some help with typedef and pointers.

Post by jdal »

Thank you both for taking the time and explaining things in detail.
Its working now.

The confusing this to me with pointers is that when you try to learn about it you see so many people doing different things.
Then is get way confusing when people to pointers to pointers. I'm like what the heck. what ever happened to the good ol'e variable[x].

I will watch your videos and hopefully this time something will stick.
Post Reply