GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Apr-2005, 21:20
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough

dereferencing void pointer


Hi,

I have a void pointer. I am mallocing to it and cast it to appropriate type. However I am getting compiler errors about dereferecing void pointer.

Following is the code

CPP / C++ / C Code:
/*
typedef struct pptp_generic_t_
{
	CARD16 length;
	CARD16 pptp_msg_type;
	CARD32 cookie;
	CARD16 control_message_type;
	CARD16 reserved0;
} pptp_generic_t;

typedef struct ctrl_msg_sccrq_t_
{
	pptp_generic_t pptp_generic_pkt;
	CARD16 version;
	CARD16 reserved1;
	CARD32 framing;
	CARD32 bearer;
	CARD16 channels;
	CARD16 firmware;
	CARD8 hostname[64];
	CARD8 vendor[64];
} ctrl_msg_sccrq_t;
*/
void *packet;

if (ptype == START_CONTROL_CONNECTION_REQUEST)
{
	packet = (ctrl_msg_sccrq_t *)malloc(sizeof(ctrl_msg_sccrq_t));
	packet->pptp_generic_pkt.pptp_msg_type=CONTROL_MESSAGE;
	packet->pptp_generic_pkt.cookie=MAGIC_COOKIE;
   
packet->pptp_generic_pkt.control_message_type=START_CONTROL_CONNECTION_REQUEST;
		packet->pptp_generic_pkt.reserved=0;


}

Compiler errors
Quote:
pclientfunction.c: In function `sendPpacket':
pclientfunction.c:116: warning: dereferencing `void *' pointer
pclientfunction.c:116: request for member `pptp_generic_pkt' in something not a structure or union
pclientfunction.c:117: warning: dereferencing `void *' pointer
pclientfunction.c:117: request for member `pptp_generic_pkt' in something not a structure or union
pclientfunction.c:118: warning: dereferencing `void *' pointer
pclientfunction.c:118: request for member `pptp_generic_pkt' in something not a structure or union
pclientfunction.c:119: warning: dereferencing `void *' pointer
pclientfunction.c:119: request for member `pptp_generic_pkt' in something not a structure or union
make: *** [pptpclient] Error 1


Can anybody help me whats going wrong here?

Thanks,
  #2  
Old 25-Apr-2005, 02:31
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 918
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
You can't dereference a void pointer, because it has an undetermined size. I only use those for returning undetermined types from functions, or storing undetermined data into a map (other people might make better use of them). To accomplish the task you set out for, you need something like this:

CPP / C++ / C Code:
if (ptype == START_CONTROL_CONNECTION_REQUEST)
{
	packet = (ctrl_msg_sccrq_t *)malloc(sizeof(ctrl_msg_sccrq_t));
	((ctrl_msg_sccrq_t *)packet)->pptp_generic_pkt.pptp_msg_type=CONTROL_MESSAGE;
	//...

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 25-Apr-2005, 02:37
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi Lucian,

Thanks for that tip. It worked. I am using void pointer because I want to use it to construct different types of packets in the same function, using packet type as handle. Each packet has different size which is not known in the begining. So I have to use void pointer for the packet and then cast it to appropriate packet type.

Thanks,
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
shortest path algorithm and file saving Pandiani C++ Forum 10 17-Jul-2006 10:46
Opinion on my code and a c++ class question FlipNode C++ Forum 7 07-Feb-2006 08:15
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35
help with classes bucho MS Visual C++ / MFC Forum 3 20-Oct-2004 06:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:15.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.