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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:14:18+00:00 2026-05-26T23:14:18+00:00

I am having a problem with strncpy() copying an extra character at the length

  • 0

I am having a problem with strncpy() copying an extra character at the length I need. I need to copy the 10 most significant bits from one string to another (both in char* format).

The size of the larger variable does not intuitively matter, and the smaller only affects the output when it is significantly smaller than the value I need (I don’t know why this is either).

The n value in my strncpy() may be what’s causing the problem, but I have no idea why it’s acting the way it is. If I’m not mistaken, n, the length of the destination string, should be 1 longer than the data, to account for the null character at the end.

My string is not behaving this way at all. Setting n to 11, I receive the following output:

00000111101100100100001110100000

00000111101

I would imagine it would only copy 10 characters, with the final one for the null. Increasing n produces the same results.

Decreasing n to 10 reveals the heart of the problem:

00000111101100100100001110100000

0000011110@

I don’t know why it would do this, but it continues to do this as n decreases, until n is much smaller (around 8).

These are the relevant lines of my code:

char line[11], hexAddr[8], binAddr[33], lv1Index[11];
    ...
strncpy(lv1Index, binAddr, 10);

where lv1Index was untouched before this, and binAddr is displayed (in the given output) directly beforehand.

The added character is always @, so I don’t believe it’s garbage from lv1Index pre-initialization.

  • 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-26T23:14:19+00:00Added an answer on May 26, 2026 at 11:14 pm

    From one C++ reference:

    If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.

    No null-character is implicitly appended to the end of destination, so destination will only be null-terminated if the length of the C string in source is less than num.

    Other documentation (MSDN, man page) has similar information.

    This means you should put the null terminator on yourself:

     lv1Index[10] = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are having problem with the server migration. We have one application that are
I am having a strange problem while returning a string. it says can not
When in doubt, turn to Stackoverflow... I'm having a problem with string allocation. My
I seem to be having a problem with extracting data from a stringstream. The
Having problem to open form in new window, script is pulled from external service.
Having problem in displaying relational properties b/w two tables having one(company) to many(package_master) relationship
Im having problem in importing hotmail contacts from hotmail in php it simple give
I'm having problem with listening to an event dispatched from a child class, and
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
We're having problem with a huge number of legacy stored procedures at work. Do

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.