struct {
char a[10];
char b[5];
char c[10];
} info;
How can I concatenate all the struct data members into one single array?
With memcpy():
// Assign a buffer big enough to hold everything
char *buf = malloc(sizeof(info.a) + sizeof(info.b) + sizeof(info.c));
// Get a pointer to the beginning of the buffer
char *p = buf;
// Copy sizeof(info.a) bytes of stuff from info.a to p
memcpy(p, info.a, sizeof(info.a));
// Advance p to point immediately after the copy of info.a
p += sizeof(info.a);
// And so on...
memcpy(p, info.b, sizeof(info.b));
p += sizeof(info.b);
memcpy(p, info.c, sizeof(info.c));
You could use sprintf. This funcions 'prints' a string into anoter:
int struct_size = sizeof(info);
char *result = (char*)malloc(sizeof(char)*struct_size);
sprintf(result, "%s%s%s", info.a, info.b, info.c);
Related
I have the following code:
typedef struct {
struct {
uint64_t key;
uint64_t hash;
uint64_t size;
uint64_t body;
} length;
} block_head;
-----------------------------------------------------
//Block allocation
uint64_t d1, d2, d4; //Lengths for octet strings to be saved in memory block
uint64_t d3; //just for saving a size - integer value
unsigned char **data = (unsigned char**)malloc(sizeof(block_head) + d1 + d2 + d4);
block_head *head = (block_head *)data;
head->length.key = d1;
head->length.hash = d2;
head->length.size = d3;
head->length.body = d4;
-----------------------------------------------------
//Later we fill memory of data block
// get a pointer key buffer location
unsigned char *d = (char *)data + sizeof(secure_head_t);
//Add octet string
FillData1(d);
// get a pointer to the body buffer location
unsigned char *body = (unsigned char *)data + (sizeof(block_head) + head->length.d1 + head->length.d2);
//get the length of the body free space (of a block)
int body_length = head->length.body;
//body is filled with octet string, and length is saved to body_length
FillBody2((unsigned char*)body, &body_length)
// Advance the body pointer to the location of the remaining space, and calculate just how much room is still available.
body += body_length;
// Save another octet string to block
FillBody3((unsigned char *)data + (sizeof(block_head) + head->length.key), &body_length);
Now I need to save the filled block (unsigned char **data) to a byte array, to restore later from array to a block.
I do this but it is not working:
unsigned char **data = some_filled_data;
block_head *head = (block_head *)data;
// convert data to arr
unsigned char *arr = (unsigned char *)malloc( sizeof(block_head) + (head->length.key + head->length.hash + head->length.body));
memcpy(&arr, data, sizeof(block_head) + (head->length.key + head->length.hash + head->length.body));
// convert arr to data
unsigned char *data = (unsigned char*)malloc( sizeof(unsigned char) * strlen(arr));
memcpy(&data, &arr, strlen(arr));
If I try to use the new converted block from arr, then I will get an error, because it is not constructed correctly or something like this
How I should convert correctly data to arr and arr to data, to represent the same block ?
While I could not decipher the description, this answers the title:
typedef struct {
int a;
char b[20];
} Test;
void fillAndBackupTest(char **out) {
Test test;
test.a = 20;
strcpy(test.b, "Hello!");
*out = (char*) malloc(sizeof Test);
memcpy(*out, &test, sizeof Test);
}
void restoreAndPrintTest(char *in) {
Test test;
memcpy(&test, in, sizeof Test);
printf("a: %d, b: %s\n", test.a, test.b);
}
int main()
{
char *blob;
fillAndBackupTest(&blob);
restoreAndPrintTest(blob);
free(blob);
return 0;
}
A structure type, Test is defined, fillAndBackupTest() creates one, fills its fields and stores a "copy" of it into a buffer it allocates for itself (it is a char* for now, but actually it could very well remain being void*), then restoreAndPrintTest() restores a (different) Test instance from this buffer and prints its contents.
What you do with malloc-ing a sum of a size and some arbitrary numbers (key? body?) does not seem to be a good idea, and strlen can not measure the length of a binary blob either.
I want to copy variable length structs to a buffer. My struct looks like this:
typedef struct{
int flag;
int size;
unsigned char name[0];
} sp;
I do not know the size of name in advance. After I get size I malloc this struct by:
sp *s = malloc(sizeof(sp)+size)
To copy to a buffer, I do this:
char *buf = calloc(1000, sizeof(*buf));
memcpy(buf, s, sizeof(sp)); //s is of type sp with all memebers initialized
My buffer remains empty. What am I doing wrong?
I don't think you want to declare name as array of pointers, but instead an array of chars.
typedef struct {
int flag;
int size;
char name[];
} sp;
Then you can create an instance like this.
int size = 10;
sp *s = malloc(sizeof(sp)+size);
s->flag = 0;
s->size = size;
strncpy(s->name, "Hello!", size);
s->name[size - 1] = '\0'; // Make sure name is NULL-terminated
You can copy the structure into a buffer as follows.
void *buf = calloc(1000, 1);
memcpy(buf, s, sizeof(s)+ s->size);
Print out the names as follows to check it worked.
printf("Name is %s.\n", s->name);
printf("The buffer's copy of name is %s.\n", ((sp*)buf)->name);
So I'm trying to write a function that will take in two pointers to chars and return a new pointer to their concatenation. Here's my attempt:
#include <stdio.h>
#include <stdlib.h>
char * append (char *, char *);
int main(void) {
char *start = "start";
char *add = "add";
char* newString = append(start, add);
printf("%s\n", newString);
return 0;
}
char * append(char *start, char *add) {
char *newArray = malloc(sizeof(char) * 100);
//get to end of start word
while (*start != '\0') {
*newArray = *start;
newArray++;
start++;
}
while (*add != '\0') {
*newArray = *add;
newArray++;
add++;
}
return newArray;
}
Two questions:
1) As of now it compiles but nothing gets printed. I think this is because my append function returns a pointer to the very end of the concatenated characters. Should I create a temporary character pointer and set it to newArray at the very start (so I can just return that)? Otherwise, I have to somehow decrement my pointer until I get back to the start. But there's no value that (like '\0' for the end of a string) that will tell me I'm at the start of the char array...
2) I read that I can't just take sizeof() a char pointer, so I'm not really sure what to pass as my argument to malloc on the first line of my append function. 100 is just a "large enough" magic number which I want to get rid of...
If I could take sizeof() a char pointer, I'd just do:
char *newArray = malloc(sizeof(strlen(start) + strlen(add)) + 1);
Thanks for the help,
bclayman
1) Yes, you need to save a pointer to the beginning of the allocated memory so that you can return it. Its better to think of that pointer as the "real" pointer and the pointer you increment as you store characters as the temporary, but it really makes no difference -- a pointer is a pointer
2) That is what strlen is for -- it tells you the length of the string. So you want
char *newArray = malloc(strlen(start) + strlen(add) + 1);
no need for sizeof at all.
With all that, you end up with:
char *append(char *start, char *add) {
char *newArray = malloc(strlen(start) + strlen(add) + 1);
if (!newArray) return 0; // out of memory
char *copy = newArray;
//get to end of start word
while (*start != '\0')
*copy++ = *start++;
while (*add != '\0')
*copy++ = *add++;
*copy = 0; // add a final NUL terminator
return newArray;
}
I want to parse a character buffer and store it in a data structure.
The 1st 4 bytes of the buffer specifies the name, the 2nd four bytes specifies the length (n) of the value and the next n bytes specifies the value.
eg: char *buff = "aaaa0006francebbbb0005swisscccc0013unitedkingdom"
I want to extract the name and the value from the buffer and store it a data structure.
eg: char *name = "aaaa"
char *value = "france"
char *name = "bbbb"
char *value = "swiss"
After storing, I should be able to access the value from the data structure by using the name.
What data structure should I use?
EDIT (from comment):
I tried the following:
struct sample {
char string[4];
int length[4];
char *value; };
struct sample s[100];
while ( *buf ) {
memcpy(s[i].string, buf, 4);
memcpy(s[i].length, buf+4, 4);
memcpy(s[i].value, buf+8, s.length);
buf += (8+s.length);
}
Should I call memcpy thrice? Is there a way to do it by calling memcpy only once?
How about not using memcpy at all?
typedef struct sample {
char name[4];
union
{
char length_data[4];
unsigned int length;
};
char value[];
} sample_t;
const char * sample_data = "aaaa\6\0\0\0francebbbb\5\0\0\0swisscccc\15\0\0\0unitedkingdom";
void main()
{
sample_t * s[10];
const char * current = sample_data;
int i = 0;
while (*current)
{
s[i] = (sample_t *) current;
current += (s[i])->length + 8;
i++;
}
// Here, s[0], s[1] and s[2] should be set properly
return;
}
Now, you never specify clearly whether the 4 bytes representing the length contain the string representation or the actual binary data; if it's four characters that needs to run through atoi() or similar then you need to do some post-processing like
s[i]->length = atoi(s[i]->length_data)
before the struct is usable, which in turn means that the source data must be writeable and probably copied locally. But even then you should be able to copy the whole input buffer at once instead of chopping it up.
Also, please note that this relies on anything using this struct honors the length field rather than treating the value field as a null-terminated string.
Finally, using binary integer data like this is obviously architecture-dependent with all the implications that follows.
To expand on your newly provided info, this will work better:
struct sample {
char string[4];
int length;
char *value; };
struct sample s[100];
while ( *buf && i < 100) {
memcpy(s[i].string, buf, 4);
s[i].length = atoi(buf+4);
s[i].value = malloc(s[i].length);
if (s[i].value)
{
memcpy(s[i].value, buf+8, s[i].length);
}
buf += (8+s[i].length);
i++;
}
I would do something like that:
I will define a variable length structure, like this:
typedef struct {
char string[4];
int length[4];
char value[0] } sample;
now , while parsing, read the string and length into temporary variables.
then, allocate enough memory for the structure.
uint32_t string = * ( ( uint32_t * ) buffer );
uint32_t length = * ( ( uint32_t * ) buffer + 4);
sample * = malloc(sizeof(sample) + length);
// Check here for malloc errors...
* ( (uint32_t *) sample->string) = string;
* ( (uint32_t *) sample->length) = length;
memcpy(sample->value, ( buffer + 8 ), length);
This approach, keeps the entire context of the buffer in one continuous memory structure.
I use it all the time.
typedef struct testMsg_ {
unsigned char opCode;
unsigned int Count;
char *macsStrList[MAC_ADDR_STR_LEN];
} testMsg_t;
Number of elements in macsStrList is m_Count.
I know following is not correct:
testMsg_t *pInput = (testMsg_t *) malloc(sizeof(testMsg_t) );
This is correct, given the structure you have done
testMsg_t *pInput = (testMsg_t *) malloc(sizeof(testMsg_t) );
However you are probably confused to the meaning of *arr[dimension] -- which is an array length dimension of pointers to chars -- reading between the lines,
MAC_ADDR_STR_LEN
Is probably ment to the legth of the string representation of a mac address (say <20 bytes?)
However your struct gives you 20 char pointers, and the character pointers still have to be initializaed to point to valid memory.
testMsg_t *pInput = (testMsg_t *) malloc(sizeof(testMsg_t) );
pInput->macsStrList[0] = (char *) malloc( MAC_ADDR_STR_LEN+1 );
pInput->macsStrList[1] = (char *) malloc( MAC_ADDR_STR_LEN+1 );
pInput->macsStrList[2] = (char *) malloc( MAC_ADDR_STR_LEN+1 );
...
or redefine your struct to
typedef struct testMsg_ {
unsigned char opCode;
unsigned int Count;
char macsStrList[NUMBER_OF_MAC_ADDRESSES][MAC_ADDR_STR_LEN];
} testMsg_t;
To avoid having to deal with multiple number of allocations.
ADDITION;
As per comments, given that the number of mac addresses are dynamically determined, you could also define the struct as;
typedef struct testMsg_ {
unsigned char opCode;
unsigned int Count;
char macsStrList[1][MAC_ADDR_STR_LEN];
} testMsg_t;
and then allocate it using
testMsg_t *pInput = (testMsg_t *) malloc(sizeof(testMsg_t) + (countOfMacsAddresses * MAC_ADDR_STR_LEN) );
That would have the added over a solution with pointers of that you could use realloc to resize the array dynamically if you needed to do that as well.....
I think what you're looking for is maybe (ok, Soren got in first, but I'll show a way to allocate a single contiguous chunk):
/* assuming we only need macStrList[0] ... [Count-1] */
struct testMsg
{
unsigned char opCode;
unsigned int Count;
char *macsStrList[];
};
struct testMsg *allocate_testMsg(int count)
{
char *string_storage;
struct testMsg *msg;
size_t size = sizeof(struct testMsg) /* base object */
+ (count * sizeof(char *)) /* char* array */
+ (count * (MAC_ADDR_STR_LEN+1)) /* char storage */
;
msg = malloc(size);
msg->Count = count;
string_storage = (char *)&(msg->macStrList[count]);
/* note msg->macStrList points to UNINITIALIZED but allocated storage.
it might be sensible to zero-fill string_storage, depending on how you'll
initialize it
*/
for (count=0; count < msg->Count;
++count, string_storage += (MAC_ADDR_STR_LEN+1))
{
msg->macStrList[count] = string_storage;
}
return msg;
}
Of course it is. You allocate a pointer to a testMsg_t which is an alias for struct testMsg_. However you need to initialize this object yourself.
(And you don't need to cast the allocated pointer in C).