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));

}

4
  • Are you asking which one of the two commented lines is correct? The memcpy should be used. Commented Jan 15, 2020 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. Commented Jan 16, 2020 at 0: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. stackoverflow.com/questions/14248044/… Commented Jan 16, 2020 at 2: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 Commented Jan 17, 2020 at 3:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The information I needed to comprehend the compound litteral syntax {.sizet={1,1,1}} is an explanation of {.sizet= a designated initializer.
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.