0

How to assign an array wrapped into a struct using the struct pointer?

I know this syntax:

size_t initArrayList[] = {1,1,1};
memcpy(pStruct->sizet, initArrayList, sizeof(pStruct->sizet));

is it possible to use a similar syntax as:

Stru = (struct myStruct) {.sizet = {1,1,1}};

using pointers?

I'd appreciate a detailed explanation of what (struct myStruct) {.sizet = {1,1,1}} does.

struct myStruct {
  size_t sizet[3];
} ;

void struInit(struct myStruct * pStruct) ;

int main()
{
struct myStruct Stru;

    struInit(&Stru);

          if (Stru.sizet[1]==1)
              printf("\nStru.sizet[1]==1");

    return 0;
}

void struInit(struct myStruct * pStruct ) {

// I know this syntax
//   size_t initArrayList[] = {1,1,1};
//   memcpy(pStruct->sizet, initArrayList, sizeof(pStruct->sizet));

}

OneArb
  • 453
  • 2
  • 14
  • Are you asking which one of the two commented lines is correct? The `memcpy` should be used. – Marco Bonelli Jan 15 '20 at 22:17
  • I am looking for a similar syntax for ```Stru = (struct myStruct) {.sizet = {1,1,1}};``` when Stru is passed by address. I know the first commented line works. – OneArb Jan 16 '20 at 00:18
  • Ive seen this before dont know enough to comment on it. I wouldnt use it in production code, but the . is to access an anonymous struct or something less intuitive/maintainable than your first answer. https://stackoverflow.com/questions/14248044/are-anonymous-structs-standard-and-really-what-are-they – Bwebb Jan 16 '20 at 02:12
  • @Bwebb The syntax is a compound literal with designated initializer. It is C99 so has been around. So yes an array can be assigned to as long it is a struct member. Ifound reading over – OneArb Jan 17 '20 at 03:14

1 Answers1

1

Either calling memcpy using a temporary array (just as commented out in your struInit function)

memcpy(pStruct->sizet, initArrayList, sizeof(pStruct->sizet));

or assigning to the pointed-to structure using a compound literal (notice the use of the * dereference operator)

*pStruct = (struct myStruct){.sizet={1,1,1}};

or even a conventional for-loop, copying element by element, does what you want.

Or if you can not rely on designated initializers or compound literals, both being features that have only been standardized since C99, you can use an equivalent, C89 way, that doesn't make use of them:

struct myStruct tmp = {{1,1,1}};
*pStruct = tmp;

I'd appreciate a detailed explanation of what (struct myStruct) {.sizet = {1,1,1}} does.

The relevant parts of C99 describing designated initializers and compound literals are 6.7.8 Initialization and 6.5.2.5 Compound literals.

The short answer is that if used in the struInit function it constructs a local unnamed object with the type struct myStruct, initializing its member sizet to {1,1,1}, this in turn being syntax for array element initialization, assigning a value of 1 to its elements [0], [1], and [2], in that exact order.

nebel
  • 171
  • 12
  • The information I needed to comprehend the compound litteral syntax ```{.sizet={1,1,1}}``` is an explanation of `{.sizet=` a designated initializer. – OneArb Jan 16 '20 at 01:10
  • I've added another method, using only C89. If you want to know more about designated initializers and compound literals, I would recommend reading the parts of the standard now mentioned in the answer, and asking a separate question about a specific syntactical quirk you want to understand. – nebel Jan 18 '20 at 02:10
  • For the sake of completeness C99 compound literals can be used in assignments, whereas the C89 equivalent only to initialize. OP stresses assignment, if you can emphasize that point so I can validate the answer. – OneArb Feb 04 '20 at 19:29