Solved

Converting a struct to a char* (EASY)

Posted on 2002-07-17
9
372 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Class Serialization, Encapsulation and Formatted Output 8 89
Safe conversion? 4 75
Why isn't object file created? 6 86
C++ help/ Toy problem 19 49
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

679 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