Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i am trying to convert the nested structure to char array using sprintf function in c programming.
struct date_of_birth{
	int dd,mm,yy;
};

struct student{
	char name[30];
	int rollNumber;
	date_of_birth dob;
};
int main()
{
// to make a single char aray using sprintf()
char arr[100];
  
}


What I have tried:

can you please let me which loop will continue first either student or dob to form char array?
Posted
Updated 8-Aug-17 11:12am
Comments
[no name] 8-Aug-17 20:59pm    
Do you want to be able to reverse the process - turn the char array back to struct?

If you need to serialize[^] the struct then memcpy[^] is the way to go

C++
student s;
char a[100];
memcpy(a, &s, sizeof(s));


[update]Fixed a blunder, thanks to K5054[/update].
 
Share this answer
 
v2
Comments
k5054 8-Aug-17 18:58pm    
That should be memcpy(a, &s, sizeof s)

Note that sizeof only needs brackets if its operand is a type rather than a variable.

It should also be pointed out that in this case the contents of a[] are not human readable.
CPallini 9-Aug-17 2:33am    
Thanks for pointing out the blunder.
I know about sizeof. It could be just a matter of style, anyway I prefer using the unnecessary brackets.
Serialized data could be (and in fact often are) not human readable.
bijaynayak 10-Aug-17 2:53am    
hi thanks for the suggestion, but according to my requirement ,
1:i need to transfer the strut data to multiple servers (or we can say diff nodes
) & that server understand only char.
2:memcpy is not suggestible because it work with address, from receiving end how to get the same structure data
CPallini 10-Aug-17 3:03am    
'memcpy is not suggestible because it work with address, from receiving end how to get the same structure data'
This is the very purpose of serialization/deserialization. Did you check out the Wikipedia page I linked?
[no name] 9-Aug-17 2:27am    
Maybe:

student s;
char a[sizeof s];
memcpy(a, &s, sizeof s)
One way :
C++
void GetStudentDataString( student * ps )
{
   const int bufferSize = 127;
   char buffer[bufferSize+1] = {0};

   snprintf( buffer, bufferSize, "%s, %d, %02d:%02d:%02d",
             ps->name, ps->rollNumber, ps->yy, ps->mm, ps->dd );
}
 
Share this answer
 
Comments
k5054 8-Aug-17 18:47pm    
Couple of notes

snprintf writes at most size bytes, including the terminating null byte to the destination, so you don't need to manually account for it.

you forgot to include the dob member when printing the date of birth. So we end up with the following:
snprintf(buffer, sizeof buffer, "%s, %d, %02d:%02d:%02d",       ps->name, ps->rollNumber, ps->dob.yy, ps->dob.mm, ps->dob.dd); 
[no name] 8-Aug-17 21:00pm    
The OP did not specify whether they wished to deserialise in which case this would not work.
bijaynayak 9-Aug-17 1:58am    
hi pwasser, yes need to deserialise the char [] to again struct.
[no name] 9-Aug-17 2:20am    
OK then why not use use memcpy() to copy struct to array and memcpy() to copy from array back to struct?
Rick York 9-Aug-17 11:30am    
The OP asked for sprintf to be used.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900