Questions tagged [strncpy]

A C standard library function: strncpy is used to copy a maximum of n characters of non-overlapping, fixed-width or null-terminated strings. Defined also as std::strncpy in C++ standard library.

Used to copy a maximum of n characters of non-overlapping, fixed-width or null-terminated strings. It is defined in the <string.h> C standard header or the <cstring> C++ standard header.

This function is not recommended to use for any purpose, neither in C nor C++. It was never intended to be a "safe version of strcpy" but is often misused for such purposes. It is in fact considered to be much more dangerous than strcpy, since the null termination mechanism of strncpy is not intuitive and therefore often misunderstood. This is because of the following behavior specified by ISO 9899:2011 7.24.2.4:

char *strncpy(char * restrict s1, 
     const char * restrict s2, 
     size_t n);

/--/

3 If the array pointed to by s2 is a string that is shorter than n characters, null characters are appended to the copy in the array pointed to by s1, until n characters in all have been written.

A very common mistake is to pass an s2 which is exactly as many characters as the n parameter, in which case s1 will not get null terminated. That is: strncpy(dst, src, strlen(src));

/* MCVE of incorrect use of strncpy */
#include <string.h>
#include <stdio.h>

int main (void)
{
  const char* STR = "hello";
  char buf[] = "halt and catch fire";
  strncpy(buf, STR, strlen(STR));
  puts(buf); // prints "helloand catch fire"
  return 0;
}

Recommended practice in C is to check the buffer size in advance and then use strcpy(), alternatively memcpy(). Recommended practice in C++ is to use std::string instead.

References:
- ISO/IEC 9899:2011 Information technology — Programming languages — C. Chapter 7.24.2.4.
- Why are strlcpy and strlcat considered insecure?
- What are the C functions from the standard library that must / should be avoided?

Documentation:
C strncpy documentation.
C++ std::strncpy documentation.

253 questions
-2
votes
3 answers

Code crashes. Trying to remove characters from char array C

I am basically trying to store everything after a certain index in the array. For example, I want to store a name which is declared as char name[10]. If the user inputs in say 15 characters, it will ignore the first five characters and store the…
Sam Thers
  • 67
  • 3
  • 12
-2
votes
1 answer

strncpy function does not work properly

what i am trying to do is asking user to enter something in format like: cd directory. Then I store "cd" in a string and "directory" in another string. Here is my code: void main() { char buf[50], cdstr[2], dirstr[50]; printf("Enter…
user3368506
  • 133
  • 1
  • 8
-2
votes
1 answer

Simple calculator in C using strncpy

Take a look at the script. It calculates telop and prints the answer. As you can see it can only calculate plus (+) now. I have never done any C coding and so I don't know how to make it calculate multiplication (X or *), minus (-) and division (:…
Mennauu
  • 157
  • 1
  • 2
  • 8
-3
votes
3 answers

Use strcpy() or strncpy() for array of strings?

I'm struggling to copy a string within an array at a given index to another array of strings, any suggestions? When trying to print out the value of tempVal at any given index, it doesn't return anything. #include #include int…
-3
votes
1 answer

strlen not producing right number and strncpy not working

I have previously gotten this to work but when I implemented functions my chars have given me a lot of issues. I have also checked if the strlen is displaying the right number, but it is displaying something weird. If I type in a 10 character long…
Matthew
  • 5
  • 5
-3
votes
1 answer

weird output using strncpy and strncat

I want to write a program that gets the first half of a string 'ch1' and puts it in a string 'ch3' then gets the first half of another string 'ch2' and concatenates it in 'ch3' "puts is in the end of ch3" but when I execute it, it gives me weird…
-3
votes
3 answers

Reversing char array

When I print out text2 I see that it is definitely not the reverse of the string I gave it and I'm not sure why that is. When I put in "test" I get stuff like "ȍ\2200+". Can I use strncpy on char arrays? Maybe it needs to be done with a loop - not…
Caleb Lawrence
  • 131
  • 1
  • 4
  • 11
-3
votes
3 answers

Alternative to strcpy? or fix to strcpy in program?

I can't figure out what is wrong with this program. I have tried using strncpy(text,array[ ],sizeof(text)) already but that didn't solve anything. What I need is a simple method of copying string like in pascal language,…
otboss
  • 621
  • 1
  • 7
  • 16
-4
votes
1 answer

C strncpy 2 char

I'm unable to make strncpy when I want to copy 2 chars from string, but same code is working when I want to copy 3 chars. In this example, I need strncpy() to store 12 in 'to' variable: void main(){ const char* from = "12345678"; char *to =…
ggoran
  • 630
  • 2
  • 12
  • 29
-5
votes
2 answers

Compilation error in strncpy in my code to find and print the longest word

I have written a program to find the longest word and to print it. My code is: #include #include #include int MaxWord(char text[],char[]); int main (void){ char text[1000]; char word[1000]; int max; …
user5536852
-5
votes
3 answers

Does under-utilized memory cause memory leak?

Does strncpy() leads to memory leak when we're copying less number of data to a larger (compile-time allocated) buffer? In other words, can the under-utilization of memory be termed as memory leak? Below is my code #define uk "ln" int main() { …
user4950013
  • 137
  • 5
-5
votes
1 answer

"strncpy_s" Not Working

I'm trying to use strncpy_s to characters from one word to an array (I cannot use strncpy in Visual Studio 2013 and I'm totally new to strncpy_s). I keep getting these errors whatever I do: Error 1 error C2660: 'strncpy_s' : function does not…
-5
votes
2 answers

Strncpy and char **

The following code doesnt copy the contents of matches 2 to keys[0]. Why is that so? char **keys; char matches[2000]; char *matches2; matches2 =strtok(matches," "); strncpy(keys[0],matches2, sizeof keys[0]);
1 2 3
16
17