Solved

Converting a struct to a char* (EASY)

Posted on 2002-07-17
9
369 Views
Last Modified: 2012-06-21
If have a struct of arrays.

struct myStruct
{
 char One[5];
 char Two[5];
 char Three[5];
 char Four[5];
};

How would I convert myStruct into a char* containing everything concatinated into one long string?  I can do it with repeated strcat() calls but thought there must be a much cleaner way to do it.

Thanks in advance.
-m
0
Comment
Question by:mudface061200
  • 6
  • 2
9 Comments
 
LVL 2

Accepted Solution

by:
kamarey earned 50 total points
ID: 7160295
struct myStruct
{
char One[5];
char Two[5];
char Three[5];
char Four[5];
};

int main()
{
    myStruct struct;

    char *p = (char*)&struct;

    ...
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 7160344
>>I can do it with repeated strcat() calls

This is in fact the way to do it, as every member of the struct is a zero-terminated C-style string, so that e.g. 'char *p = (char*)&struct;' would only render the 1st member...

Alternatively, you could use 'sprintf()' or a stringstream, e.g.

struct myStruct
{
char One[5];
char Two[5];
char Three[5];
char Four[5];
};

char* StringFromStruct ( struct myStruct* p, char* buf, unsigned long bufsize) {

 if ( bufsize < sizeof ( struct myStruct)) return NULL;

 sprintf ( buf, "%s %s %s %s", p->One, p->Two, p->Three, p->Four);

 return buf;
}
0
 
LVL 1

Author Comment

by:mudface061200
ID: 7160392
Great!  That's exactly what I needed. Thanks for the quick reply!

-m
0
 
LVL 86

Expert Comment

by:jkr
ID: 7160397
Er, ????
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 86

Expert Comment

by:jkr
ID: 7160406
>>That's exactly what I needed

If you mean 'char *p = (char*)&struct;' - that won't work...
0
 
LVL 86

Expert Comment

by:jkr
ID: 7160677
No response?
0
 
LVL 1

Author Comment

by:mudface061200
ID: 7160953
char *p = (char*)&struct;

Works just fine.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7161060
*shrug* :o)
0
 
LVL 86

Expert Comment

by:jkr
ID: 7161061
BTW, sorry to have bothered you - if it works for you, it is just fine :o)
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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 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

22 Experts available now in Live!

Get 1:1 Help Now