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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:23:59+00:00 2026-05-28T02:23:59+00:00

I read about __packed__ from here and, I understood that when __packed__ is used

  • 0

I read about __packed__ from here and, I understood that when __packed__ is used in a struct or union, it means that the member variables are placed in such a way to minimize the memory required to store the struct or union.

Now, consider the structures in the following code. They contain same elements (same type, same variable names and placed in the same order). The difference is, one is __packed__ and the other is not.

#include <stdio.h>

int main(void)
{

    typedef struct unpacked_struct {
        char c;
        int i;
        float f;
        double d;
    }ups;

    typedef struct __attribute__ ((__packed__)) packed_struct {
        char c;
        int i;
        float f;
        double d;
    }ps;

    printf("sizeof(my_unpacked_struct)  : %d \n", sizeof(ups));
    printf("sizeof(my_packed_struct)    : %d \n", sizeof(ps));

    ups ups1 = init_ups();
    ps ps1;

    return 0;
}

Is there a way where we can copy unpacked structure ups1 into packed structure ps1 without doing a member-variable-wise-copy? Is there something like memcpy() that is applicable here?

  • 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-05-28T02:24:00+00:00Added an answer on May 28, 2026 at 2:24 am

    I’m afraid you’ve just gotta write it out. Nothing in standard C (or any standard I know of) will do this for you. Write it once and never think about it again.

    ps ups_to_ps(ups ups) {
        return (ps) {
            .c = ups.c,
            .i = ups.i,
            .f = ups.f,
            .d = ups.d,
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Read about Server push here . I want to push data to client from
I read about Big-O Notation from here and had few questions on calculating the
I read about using the CASE expression inside the WHERE clause here: http://scottelkin.com/sql/using-a-case-statement-in-a-sql-where-clause/ I'm
I read about an inheritance feature in PostgreSQL that seemed pretty neat. Unfortunately I
I'm just reading about TCP and UDP and from what I've read when the
I read that the order of bit fields within a struct is platform specific.
I've read a number of articles about UDP packet sizes but have been unable
I read about the Conditional attribute today. According to MSDN: Applying ConditionalAttribute to a
Ive read about it and to be honest it all seems like a bunch
I read about small talk being completely object oriented.. is C++ also completely object

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.