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

  • Home
  • SEARCH
  • 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 937255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:26:52+00:00 2026-05-15T21:26:52+00:00

I have a char buf[x] , int s and void* data . I want

  • 0

I have a char buf[x], int s and void* data.

I want to write a string of size s into data from buf.

How can I accomplish it?

Thanks in advance.

  • 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-15T21:26:53+00:00Added an answer on May 15, 2026 at 9:26 pm

    Assuming that

    • by “string” you mean a null-terminated string as is normally meant in C;
    • you haven’t yet allocated memory in data;
    • you already know that s <= x

    First you need to allocate memory in data. Don’t forget the room for the 0 byte at the end of the string.

    data = malloc(s+1);
    if (data == NULL) {
        ... /*out-of-memory handler*/
    }
    

    Assuming malloc succeeds, you can now copy the bytes.

    EDIT:

    The best function for the job, as pointed out by caf, is strncat. (It’s fully portable, being part of C89.) It appends to the destination string, so arrange for the destination to be an empty string beforehand:

    *(char*)data = 0;
    strncat(data, buf, s);
    

    Other inferior possibilities, kept here to serve as examples of related functions:

    • If you have strlcpy (which is not standard C but is common on modern Unix systems; there are public domain implementations floating around):

      strlcpy(data, buf, s+1);
      
    • If you know that there are at least s characters in the source string, you can use memcpy:

      memcpy(data, buf, s);
      

      ((char*)data)[s+1] = 0;

    • Otherwise you can compute the length of the source string first:

      size_t bytes_to_copy = strlen(buf);
      if (bytes_to_copy > s) bytes_to_copy = s;
      memcpy(data, buf, bytes_to_copy);
      ((char*)data)[s+1] = 0;
      
    • Or you can use strncpy, though it’s inefficient if the actual length of the source string is much smaller than s:

      strncpy(data, buf, s);
      ((char*)data)[s+1] = 0;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.