0

So I have a struct with arrays inside it like so:

struct struct1 {
unsigned char   data1[32];
unsigned char   data2[32];
char   *id;
};

and a second struct defined as

typedef struct
{
   uint8_t id;  
   uint8_t data1[32]; 
   uint8_t data2[32]; 
} struct2;

Struct1 with data already inside it is passed to me via a function like so:

bool func1(struct struct1 * const struct1)

and I need to create a NEW struct2 and pass all the data from struct1 into it.

I thought I could just assign the pointers like so

struct2 *new_struct;

new_struct->id = struct1->id;
new_struct->data1 = struct1->data1;
new_struct->data2 = struct1->data2;

but I guess array pointers in C cannot be changed (or at least that's what I got from reading up on it).

So how do I create a new struct2 and pass the data I need into it from struct1?

1
  • memcpy(struct1->data1, new_struct->data1, size_of(struct1->data)) Commented Jun 11, 2018 at 2:23

1 Answer 1

1

array pointers in C cannot be changed

There is no such thing as an "array pointer". Either you have an array, or you have a pointer.

In your case data1 and data2 are arrays, so there is no pointer you could have reassigned. Your only choice is to copy the data stored in the arrays from one struct to the other.

You can use simple assignment (=) between struct variables of the same type, but in your case you have different types, so you need to copy each member separately. The easiest way to do that is to use memcpy (from <string.h>).

#include <string.h>

// ...
struct2 new_struct;

new_struct.id = *struct1->id;
memcpy(new_struct.data1, struct1->data1, sizeof new_struct.data1);
memcpy(new_struct.data2, struct1->data2, sizeof new_struct.data2);

Notes:

  • new_struct is not a pointer here. In your example you dereference an uninitialized pointer, which has undefined behavior.
  • I dereferenced struct1->id because struct2.id is a single char, not a pointer. I assume this is what you want to happen.
Sign up to request clarification or add additional context in comments.

4 Comments

Well, there is something called array pointer, but it is something else than this example. An array pointer pointing at an array unsigned char data1[32]; is declared as unsigned char(*ptr)[32]. The reason why the OP can't use simple assignment between struct variables of the same type, is because they are arrays, and C doesn't allow assignment of arrays.
Quibble, but I know what you mean. "array pointer", "pointer to array", I see them used interchangeably, but believe the latter to be the correct verbiage. (at least in my mind it unwinds to "pointer to array of type char[32]")
As I understand it, data1 itself IS the address pointer to the memory where the first element is stored. So you can do something like unsigned char *ptr = data1, and ptr[0]==data1[0] ?
@electronuts Your conclusion is correct (you can do unsigned char *ptr = data1), but your assumption is not: data1 is not an address or pointer; it is an array (and if you look at the memory, an array is its elements; there is no extra structure). The reason you can treat it as a pointer is that there is an implicit conversion: Whenever you use an array in an expression (and it is not the operand of & or sizeof), you get a pointer to its first element instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.