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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:08:43+00:00 2026-05-29T09:08:43+00:00

Please, tell me, what is the correct way of copying allocated char array to

  • 0

Please, tell me, what is the correct way of copying allocated char array to “normal” char array?
I have attempted to do the following, but it fails :

char * buf = (char *) malloc (BUFSIZ * sizeof(char));
// filling up the allocated array with stuff...
char temp[BUFSIZ];
strcpy(temp, buf); // strcpy doesn't work
  • 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-29T09:08:44+00:00Added an answer on May 29, 2026 at 9:08 am

    First things first, you should not cast the return value of malloc (in C anyway) since it can hide errors.

    Secondly, you never need to multiply by sizeof(char) since it’s always guaranteed to be one – doing so clogs up your code.

    And, as to the actual question, you can use:

    memcpy (temp, buff, BUFFSZ);
    

    to copy the entire character array.

    I’m assuming that’s what you want because you make no mention of handling C “strings”, only a character array.

    If indeed you are handling C strings, strcpy will work fine in this case, provided:

    • you have room at the end of the buffer for the terminating zero-byte; and
    • you’ve actually put the zero-byte in there.

    For example, this little snippet works fine:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (void) {
        // Two buffers.
    
        char buff1[4];
        char *buff2 = malloc (4);
        if (buff2 == NULL) {
            puts ("No memory!");
            return 1;
        }
    
        // Populate first buffer.
    
        buff2[0] = 'p';
        buff2[1] = 'a';
        buff2[2] = 'x';
        buff2[3] = '\0';
    
        // Transfer and print.
    
        strcpy (buff1, buff2);
        puts (buff1);
    
        // Free and exit.
    
        free (buff2);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please tell me the correct way to modify the following: $(#datepicker01).datepicker({ numberOfMonths:
I have the following code: volatile unsigned int * address; char * array=0x10000008; Please
Please tell me if it is possible to do the following: create an instance
Could you guys please tell me how I can make the following code more
I have added two tables in my Magento Config.xml file. please tell me is
Can somebody please tell me which is the better way, they both produce the
Could someone please tell me what is exactly wrong with the way my case
Which is the correct way to load an Iframe using JQuery? Actually I have
I have created a custom Payment module but I could only display the following
Please tell me there is a better solution to querying a remote Access database!

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.