Solved

Converting a struct to a char* (EASY)

Posted on 2002-07-17
9
368 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

23 Experts available now in Live!

Get 1:1 Help Now