troubleshooting Question

C Structure to Char pointer, failure

Avatar of fraW
fraWFlag for Sweden asked on
C
30 Comments1 Solution3502 ViewsLast Modified:
The code below is used in order to try and cast / convert a structure to a char pointer. Why do i want to do this? I want to print the representing bytes so that i afterwards can cast from a string to a structure.

The following code prints the memory address, and not the representation itself. Suggestions?
#include <string.h>
 
typedef struct NetworkFrame_
{
      unsigned ack : 1;
      unsigned tseq : 7;
} NetworkFrame;
 
void main(void)
{
      NetworkFrame nf;
      NetworkFrame * pnf;
      
      char* test;
      
      nf.ack = 1;
      nf.tseq = 12345678;
      
      test = (char*)&nf; // Here you can't the address not the object itself!
 
      pnf = (NetworkFrame *) test;
 
      memcpy(&nf,  test, sizeof(NetworkFrame));
 
	  printf("%x", test);
 
	  system("PAUSE");
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 30 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 30 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros