Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:49:45+00:00 2026-06-13T16:49:45+00:00

How to assign c type array to struct member that is also an array

  • 0

How to assign c type array to struct member that is also an array of the same type? Here’s my struct:

typedef struct {

  uint8_t type;
  uint8_t data[10];

} MyStruct;

Here’s the creation of the struct:

MyStruct myStruct;

Here’s generation of some array:

uint8_t generatedArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Here’s my assignment:

myStruct.data = generatedArray;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:49:47+00:00Added an answer on June 13, 2026 at 4:49 pm

    As other answers have stated an array is not directly assignable and you must use a function such as memcpy:

    memcpy(myStruct.data, generatedArray, sizeof(myStruct.data));
    

    However…

    C is a language with a long history. Early languages had roughly the notion of “little” and “large” types, the former being directly assignable and the latter not. Some later languages made everything assignable (or for functional languages, values).

    Original C had primitive types; integer, float, etc.; and arrays of these types. The former, being “little”, were assignable, the latter, being “large”, were not. Subsequently C gained structures, and decided these were “little”…

    So, a little strangely, while you cannot directly assign one array to another you can assign on structure to another, and in particular you can assign a structure with just one field which is an array… You can also write literal structure values, including for those with array-valued fields; and being C with its “types are comments” approach you can cast a pointer to an array to a pointer to a structure…

    The above means you can write “fun” code like:

    typedef struct
    {
        uint8_t type;
        uint8_t data[10];
    } MyStruct;
    
    typedef struct
    {
        uint8_t data[10];
    } MyArray;
    
    typedef struct
    {
        uint8_t type;
        MyArray array;
    } MyStruct2;
    
    void arrayAssignment(int x)
    {
        MyStruct myStruct;
        MyArray generatedArray = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}; // structure literal
    
        *(MyArray *)&myStruct.data = generatedArray;
    
        uint8_t generatedArray2[] = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; // array literal
        *(MyArray *)&myStruct.data = *(MyArray *)generatedArray2;
    
        MyStruct2 myStruct2;
        myStruct2.array = generatedArray; // no casts at all, but element access is myStruct2.array.data[index]
    }
    

    Note that none of the casts, indirections, etc. or in the final example the extra array. in the indexing cost anything in the compiled code – they are all simply ways to persuade the compiler to use its built-in array assignment.

    Many would of course argue against regular uses of these techniques!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can we assign null value to struct type of variable? struct MyStruct { }
I have a SOAP service that returns an array of a complex type. The
I'm totally new to Scala. Here I have tried to assign a empty array
When i assign a key value into an array of type string, does it
I am trying to assign a custom type as a key for std::map .
I need to get UserCarId(int) from this query and assign to int type variable.
//assign variables string measurementIn; //read in file in array string[] lines = File.ReadAllLines(../../convert.txt); //ask
To assign specific value to 1D array I'm using LINQ like so: int[] nums
I have an array called $mydata that looks like this: Array ( [0] =>
I declare a Byte-Array like this: Byte[] b = new Byte[10]; and assign some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.