?
Solved

Converting a struct to a char* (EASY)

Posted on 2002-07-17
9
Medium Priority
?
375 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 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