Reputation: 31
How can I typecast a string to a structure which has char array members of different sizes? for example
typedef struct format{
char test1[12];
char test2[3];
char test3[3];
char test4[3];
char test5[9];
char test6[3];
char test7[3];
char test8[7];
char test9[24];
char test10[3];
char test11[24];
}format;
Now I will have one string which will be concated with above information & I simply want to parse the respective information from them and fill it to respective structure members.
Upvotes: 0
Views: 2299
Reputation: 8607
What's your rationale? You might be able to get away with:
if C :
// s is a null-terminated c string.
format* f = (format*)s;
that's all there is to it.
if C++ :
// std::string s; // assuming basic_string<char>
// do stuff with s
format* f = reinterpret_cast<format*>(s.c_str());
Provided the string stays in scope. This might be risky depending on your STL implementation.
For both C++ and C: You also run the risk of reading past each string's extent if your string slices are not zero terminated and you call standard c-style string functions on the members of your format struct.
As undur_gongor and glglgl pointed out, this may not work if the alignment and padding of the char arrays inside the struct result in a non-tightly packed struct (i.e. there is extra padding as a result of alignment rules).
As Steve pointed out, On most all architectures a char
is 1 byte, meaning you should be safe.
Upvotes: 2
Reputation: 2368
In C, there is nothing stopping you from casting this struct to a char* and treating it like a string.
Having said that however, you are going to have to be careful how you treat this "string". Consider the following example:
typedef struct format{
char R[12];
char M[15];
} format;
void main(){
format A;
int i;
strcpy(A.R,"AA");
strcpy(A.M,"AABBCC");
char *B=(char*)&A; //Note the typecasting
printf("%s",B);
for (i=0;i<strlen(B);i++){
printf("%d\n",*(B+i));
}
}
The example above is not going to work as expected, i.e. it is not going to iterate over all the characters within A because strings in C are null terminated (http://en.wikipedia.org/wiki/Null-terminated_string) and this is how various functions (strlen, strcpy and others) know that they have reached the end of a stream of chars. So, the above code will output "AA" and [65,65] (which is the asci equivalent of "A").
For the above piece of code to do what it is expected to do, we would have to EITHER:
1) Remove all "\0" (null) characters from A EXCEPT the last one, prior to treating it as a string (and feeding it to other functions like printf for example)
2) Or, if we want to maintain all individual fields as strings and still parse it, we would now have to use sizeof(format) (or sizeof(A)) instead of strlen(B) in the "for" iteration above.
So, yes you can typecast a struct of chars to a (char *) and treat it like a string but there's a bit of preprocessing you are going to have to do for this to work as expected. You could for example have a function that implements the removal of "\0"s as in (1) above, called something like "structToString".
Upvotes: 0