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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:48:05+00:00 2026-05-26T11:48:05+00:00

function(char *a, char *b) { char newStr[100]; strncpy(newStr, a, sizeof(newStr)); //Line 1 – copy

  • 0
function(char *a, char *b)
{

   char newStr[100];

   strncpy(newStr, a, sizeof(newStr)); //Line 1 - copy no more than 100 bytes  

   strncat(newStr, b, (sizeof(newStr) -  strlen(newStr)));   //Line 2 - ?

   newStr[99] = NULL; //Line 3 - null terminate string

}

Line 2: Correct to specify 100 bytes minus strlen of what a copied over from a to ensure I don’t copy over the 100 bytes?

Thank You.

  • 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-26T11:48:05+00:00Added an answer on May 26, 2026 at 11:48 am

    This is almost correct. First, the NUL-termination line:

    newStr[99] = NULL;
    

    is wrong.

    strncat always NUL-teriminates, and the third parameter is the maximum number of bytes to write NOT including the NUL.

    Hypothetically, if strncat didn’t NUL-terminate, the problem with that line is that it would always write to the last element of the array, even though the actual string could be much shorter. If a and b were “Hello, ” and “world!”, the final array would be:

    H|e|l|l|o|,| |w|o|r|l|d|!|g|i|b|b|e|r|i|s|h

    where gibberish represents the previous contents of the array at those positions. Only at 99, after most of these uninitialized remnants, would there be a NUL.

    EDIT: Also, Keith is correct about strncpy. His function is partly right, but that the second function can overflow the buffer, since it doesn’t take into account the string that’s already there. The two lines combined can write 199 characters (including NUL).

    Also, I was wrong about the third parameter including the NUL. That leaves us with (modified from Keith’s):

    void function(char *a, char *b)
    {
        char newStr[100];
    
        /* Make newStr an empty string so you can catenate onto it */
        newStr[0] = '\0';
        strncat(newStr, a, sizeof newStr - 1);
        strncat(newStr, b, sizeof newStr - strlen(newStr) - 1);
    
        /* Presumably you do something with newStr here */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have function: char *zap(char *ar) { char pie[100] = INSERT INTO test (nazwa,
In Guile 1.6.*, the function scm_istring2number(char *str,int strlen,int radix) does the work. However, this
this doesn't work: void function(char* var) { var = (char*) malloc (100); } int
i have this function char* copy(char* pString,...){ char *vToate; int vLen; va_list vAp; va_start(vAp,pString);
I have a function: char *make_text(void) { char txt[MAXLEN]; //make something return txt; }
In the following code I get a segmentation fault: Set *getpar() {...} char function(...)
I have this function: void ToUpper(char * S) { while (*S!=0) { *S=(*S >=
my function: struct hostent * gethost(char * hostname){ if(/*some condition under which I want
Given the following C function in a DLL: char * GetDir(char* path ); How
I have a function that accepts a char* as one of its parameters. I

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.