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 8283177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:38:46+00:00 2026-06-08T10:38:46+00:00

I have an array of 9 bytes and I want to copy these bytes

  • 0

I have an array of 9 bytes and I want to copy these bytes to a structure :

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct _structure {
    char one[5];        /* 5 bytes */
    unsigned int two;   /* 4 bytes */
} structure;

int main(int argc, char **argv) {

    structure my_structure;

    char array[]    = {
        0x41, 0x42, 0x43, 0x44, 0x00,   /* ABCD\0 */
        0x00, 0xbc, 0x61, 0x4e          /* 12345678 (base 10) */
    };

    memcpy(&my_structure, array, sizeof(my_structure));

    printf("%s\n", my_structure.one);   /* OK, "ABCD" */
    printf("%d\n", my_structure.two);   /* it prints 1128415566 */

    return(0);
}

The first element of the structure my_structure, one, is copied correctly; however, my_structure.two contains 1128415566 while I expect 12345678. array and my_structure have different sizes and even if they are equal in size, still there will be a problem with two . How can I fix this issue?

  • 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-08T10:38:47+00:00Added an answer on June 8, 2026 at 10:38 am

    As Mysticial already explained, what you’re seeing is the effect of structure alignment – the compiler will align elements on boundaries of its word size, ie in 32 bits code on 4-byte boundaries, effectively leaving a gap of 3 bytes between the char[5] and the next element.

    If you use gcc or Visual Studio, #pragma pack(1) allows you to override the “preferred” packing the compiler would use by default – in this example you instruct the compiler to instruct on 1-byte boundaries, ie without “holes”. This is often useful in embedded systems to map blocks of bytes onto a structure. For other compilers consult your compiler manual.

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

Sidebar

Related Questions

I have a char type array[100] with 100 bytes stored in it. I want
I want to copy a string into a char array, and not overrun the
Now I have an array of 7 bits bytes or a string I want
I have an array of bytes and i want to determine if the contents
I have a byte array which i want to copy/clone to avoid calling code
I have the following problem. I have an array of bytes that I want
I have an array of bytes. I want to simulate corrupt data by changing
I have an array of bytes (any length), and I want to encode this
I want to convert a string into a byte array. (Yes) I have seen
I have one array of bytes. I want to access each of the bytes

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.