Solved

Typecasting char* to a structure

Posted on 2006-11-03
7
1,257 Views
Last Modified: 2010-08-05
Situation:

I have created an application which sends a structure (composed mostly of DWORD, UWORD, etc...) from one PC to another. Take into mind that the structure of the message also uses nested structures (meaning structures within the main structure). This communication is made possible by windows socket. The communication of socket message passing (send() and recv()) uses char pointer. So, to make the message pass through the other side, I typcasted my message into char pointer.

Problem:
After being received by the recv() function, the message is in char pointer. How can I convert the char pointer back into the structure.

Actions taken:
1. Type casted inot structure directly: structure = (structure)buffer, structure = (structure)buffer;
2. Changed the message passed into plain character. The message was displayed correctly, meaning, the message passed is correctly sent.
3. Used memcpy. As expected, it failed.
4. Changed the receiving structure into a structure pointer: structure* = (structure*)buffer, structure* = (structure*)&buffer -->nice try.
5. I even tied typecasting it to void.


Your help will greatly be appreciated. Thank you.
0
Comment
Question by:Mauaka
7 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 165 total points
ID: 17872492
struct SubStructure
{
    int a;
    float b;
};

struct AnyStructure
{
    int n1;
    int n2;

    Substructure s;
};

void FillStructure(char* buffer)
{
    AnyStructure s;
    memcpy((void*)&s, (const void*)buffer, sizeof(AnyStructure));
    ...
}

If this doesn't work, show how you send this structure.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 17873532
Copy isn't even necessary, just use something like:

  struct AnyStruct * anyPtr;

  anyPtr = (struct AnyStruct *) buffer;
0
 
LVL 7

Expert Comment

by:yotamsher
ID: 17873705
Hey  Mauaka

I think your problems, might start, at the sending side:

if you cast a struct pointer, with pointers to other struct inside, into a char *, the result, will not contain the sub structs.
could you post the details, of the structs, and the sending code.

Yotam
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:jhshukla
ID: 17887211
yotam, do not worry. if you look at the prototype it is [code]int recv(SOCKET s,  char* buf,  int len,  int flags);[/code]

HonorGod, your answer is syntactically correct but imagine what would happen when recv is called second time with same buffer?

recv(sock, (char*)&some_struct, len, flags) would save some time over using FillStructure().
0
 
LVL 2

Expert Comment

by:numansiddique
ID: 17895502
suppose if your structure is

struct Anystruct
{
  int a;
 char *str;
};
If your structure contains pointer variables, then i think when you cast it to char and send it, you are actually sending the pointer address and not the value pointed to by the pointer. when you receive the data at the other end ie other pc, and cast it to structure  the pointer variable is no longer valid.

This could be a problem.
please any body correct me if i am wrong.
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 17895568
true numansiddique. I guess the programmer has to take care of that manually.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 17898087
 The question was posed in such a way as to be very specific.  So the answer that was provided was also specific.
Any variable can be overwritten.  C and C++ are generally considered mid level languages because they are not
far from the architecture of the hardware.  C was originally designed to be a high-level assembly language for the PDP process to which K & R had access.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
countX 22 84
Sorting in Excel with Group Headers if the Exist 2 82
countAbc challenge 9 59
Turning python script into an applet 12 104
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now