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
-1
votes
1 answer

My string concatination is doubling its result each time, why?

I'm basically just taking a string and appending / concatting with another string. The first run through produces the desired results, but the 2nd, 3rd and so on results seem to be doubling the src string. Combining things with jQuery is super…
user3622460
  • 1,231
  • 5
  • 23
  • 42
-1
votes
3 answers

Isn't strncpy better than strcpy?

I know there are a lot of answers on the web which say one is better than the other but I have come to this understanding of strncpy() is better in terms of preventing unnecessary crashes in the system because of the missing '\0'. As many people put…
haveri
  • 29
  • 1
  • 2
-1
votes
2 answers

encountering exception when copying string using strncpy

I have a string that I'm iterating through looking for a particular word, which happens to be in-between two white-spaces. For example: // where the word that I'm looking for is /docs/index.html const char* c = "GET /docs/index.html…
Strahinja Ajvaz
  • 2,521
  • 5
  • 23
  • 38
-1
votes
3 answers

Using strncpy to remove part of a char*

I am trying to remove a certain part of my string using strncpy but I am facing some issues here. This is what my 2 char* has. trimmed has for example "127.0.0.1/8|rubbish|rubbish2|" which is a prefix of a address. backportion contains…
Bocky
  • 483
  • 1
  • 7
  • 27
-1
votes
4 answers

strncpy from **char to *char[] not copying last element

I'm trying to copy the individual elements of argv into a 2nd array. I'm using strncpy to do it. Here's a stripped version that replicates the problem. #include #include int main(int argc, char *argv[]) { printf("%d\n",…
Jack Galt
  • 83
  • 8
-1
votes
2 answers

Why does the string I copied using strncpy have junk instead of the last character?

I malloc'd an array of structures called "locations". In said structure is an element called "country". I created a string you can see below that holds "United States" in it. I malloc'd space to hold the string (it's required that I do so) and…
Tyler Shellberg
  • 1,086
  • 11
  • 28
-1
votes
1 answer

stack smashing, cant find overflow error

I'm trying to write a function that will pad a string with some character if the length of the string is less than the max size allocated to the char buffer. I'm encountering a "* stack smashing detected *: ./test terminated" error which halts my…
tovry
  • 1
  • 2
-1
votes
2 answers

string.h and strncpy with pointers in C

I'm trying to create a user input created list that contains a structure with one int and two strings. But i seem unable to use correctly the strncopy from the string.h. I'm supposed to use the order of the parameters like: 1. name of pointer 2.…
Mimic01
  • 107
  • 1
  • 12
-1
votes
4 answers

Own strncpy() C++

I am trying to implement my own version of strncpy(), i found a source code from this link. But I encountered a Unhandled exception at 0x00411ad5 in exercise 2.exe: 0xC0000005: Access violation writing location 0x00417800. everytime the code reaches…
chrisjohn016
  • 75
  • 2
  • 10
-1
votes
1 answer

Find path for parent folder

I'm trying to parse a string in c something like this : /afolder/secondfolder/thirdone do a function and that function should return this: /afolder/secondfolder I've tryed many things... int getParentFolder(const char *pPathNewLink, char*…
Lorac
  • 395
  • 1
  • 4
  • 14
-1
votes
1 answer

strncpy and _tcsncpy c++ example

I couldn't find any clear source in google, so I decided ask here. Functions strncpy and _tcsncpy in c++ accept 3 arguments: destination, source and nCount. It is not clear for me what is 3rd parameter nCount. Is it amount of characters to be copied…
Nurlan
  • 2,860
  • 19
  • 47
  • 64
-2
votes
3 answers

C Reason why strncpy not have null termination

I'm currently learning strings in C, and a question arose: why doesn't the C developers make strncpy null terminated automatically? For example in the implementation of strncpy, why didn't people who make C language add this line: // dest denotes…
Falanke
  • 69
  • 5
-2
votes
1 answer

C - Copy a char into a char*

I got some troubles while I'm trying to set a char into a char* ("string") I got lines, which is all the txt lines I fetched before, and now I'm trying to assign a char to my char*, to filter it. Here is my actual code : void TreatDatas(char**…
Izio
  • 378
  • 6
  • 15
-2
votes
1 answer

Combine const char with string into another const char

I've got two const char [] PROGMEM variables that I need to concatenate into another const char [] PROGMEM. I'm totally new to C and couldn't understand how to use strncpy on a previous question. const char data_one[] PROGMEM =…
Sam Denty
  • 3,693
  • 3
  • 30
  • 43
-2
votes
1 answer

Can't copy a part of a string

I was wondering how I can copy a part of a string to another one, so I checked this web to see if I could get an answer, and I did: How can I copy part of another string in C, given a starting and ending index However, when I use this strncpy(dest,…
Jokeiis
  • 11
1 2 3
16
17