[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Converting a struct to a char* (EASY)

Posted on 2002-07-17
9
Medium Priority
?
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 2

Accepted Solution

by:
kamarey earned 200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

649 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