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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:53:32+00:00 2026-05-31T02:53:32+00:00

I want to write in 2 chars and a bit vector (uint64_t) to a

  • 0

I want to write in 2 chars and a bit vector (uint64_t) to a file, but I first have to write them all to a buffer. Then the buffer will be written to the file. How should I write these 3 variables into a buffer (void pointer) so that all can be contained within one (void pointer) variable.

For example I want to write

char a = 'a';
char b = 'b';
uint64_t c = 0x0000111100001111;

into

void *buffer = malloc(sizeof(char)*2+sizeof(uint64_t));

Then write that into a file using

write(fd, buffer, sizeof(char)*2+sizeof(uint64_t));
  • 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-31T02:53:33+00:00Added an answer on May 31, 2026 at 2:53 am

    This is the (almost*) completely safe way of doing it:

    uint8_t *buffer = malloc(2 + sizeof(uint64_t));
    buffer[0] = a;
    buffer[1] = b;
    memcpy(buffer + 2, &c, sizeof(c));
    

    You might be tempted to do something like *(uint64_t *)(buffer + 2) = c; but that’s not portable due to alignment restrictions.

    Note that sizeof(char) == 1, per definition in the C standard.

    (*) I’ve assumed 8-bit char, which is nearly, but not entirely universal; on a platform with 16-bit char, use memcpy for a and b as well.

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

Sidebar

Related Questions

i have an char array b[20] which i want to write into a file
I want to write a syntax highlighting extension for Emacs, but I Googling of
I want to write a JAR file that makes use of the javax servlet
I want to write to a file in the windows temp directory. I know
I'm trying to append some data to a file, but in some cases want
I want to write a odometer-like method in a C#-style-language, but not just using
I have a bit of a weird problem here! I am trying to write
So, I have an array of unsigned chars, currently I'm trying to write a
I have a 256-bit value in Verilog: reg [255:0] val; I want to define
I want write a little code analyzer which parses nested structures and translates into

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.