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
0
votes
3 answers

Malloc and strncpy

I am trying to split a string in two to pass to two children. the first part should be 12 '0' characters and the second should be 13 '1' characters. When I execute this code, I do not get either of these things. specifically this is the…
ReezaCoriza
  • 133
  • 4
  • 16
0
votes
5 answers

C copy char * to char[]

Hello I am trying to copy a char * pointer to a char [] array. this is my code so far char * string_add(char * base, char * toAdd) { char * string=malloc(strlen(base)+streln(toAdd)+1); sprintf(string,"%s%s",base,toAdd); char…
SevenOfNine
  • 630
  • 1
  • 6
  • 25
0
votes
6 answers

Replace char in string with another string in C

I'm trying to replace ' ' (space) with '___' (triple underscore) in C. Here is my code: #include #include #include int main() { char *a = "12 34 56"; int a_l = strlen(a); printf("str1: \"%s\" (%d)\n", a,…
tpimh
  • 446
  • 2
  • 9
  • 23
0
votes
3 answers

Not working / with own Strncpy function

I'm having probs with an own function that should make str2 copied to str1 based on the amount of characters. char * strncpy_own(char * str1, char * str2, int c) { int i; for( i = 0; i < c; i++, str1++, str2++ ) { *str1 = *str2; …
0
votes
3 answers

Output wrong. Possible strncpy issue?

So, I'm trying to get this code to parse each line inputted from the file into individual tokens, then add each one in turn to tklist array. Then the main just prints out each token. It's printing blanks though, and when I step into the code it…
singmotor
  • 3,930
  • 12
  • 45
  • 79
0
votes
1 answer

c++ c-strings, strncat, strncpy

This program is supposed to input someones name and output it like " Last, first middle". The names are supposed to be stored in 3 different arrays and their is a fourth array for the full name at the end. I am also supposed to use strncpy and…
user1807815
  • 83
  • 2
  • 12
0
votes
3 answers

strncpy() core dump when I use Libtar in ubuntu12.04

I trace the coredump in the encode.c:33. The source code like this: if (t->options & TAR_GNU) strncpy(t->th_buf.magic, "ustar ", 8); // here is the coredump …
Yifan Wang
  • 504
  • 6
  • 13
0
votes
2 answers

strncpy to already created char []

There is class class Cow{ char name[20]; char* hobby; double weight; public: [..] Cow & operator=(const Cow &c); [..] }; and I'm wondering how to write definition of operator= method. I wrote definition that equal to - Cow…
Filip Bartuzi
  • 5,711
  • 7
  • 54
  • 102
0
votes
1 answer

assign value to pointer char?

struct group { char *name; struct user *users; struct xct *xcts; struct group *next; }; int add_group(Group **group_list_ptr, const char *group_name) { printf("%p\n",group_list_ptr); *group_list_ptr = malloc(sizeof(struct…
RandomGuy
  • 4,949
  • 4
  • 16
  • 15
0
votes
4 answers

strncpy is not working as expected

#include using namespace std #include int main(){ char token[] = "some random string"; char c[23]; strcpy( c, token); strncpy(c, token, 5); c[strlen(c)] = '\0'; cout<
Viku
  • 2,845
  • 4
  • 35
  • 63
0
votes
3 answers

Why does this strncpy() implementation crashes on second run?

Why does this strncpy() implementation crashes on second run, while the first run works ok? strncpy Copy characters from string Copies the first n characters of source to destination. If the end of the source C string (which is signaled by a…
tempy
  • 897
  • 3
  • 14
  • 22
0
votes
2 answers

Bus Error with strncpy in C

I am working on the same project as in this question, however with a slightly different typedef: typedef struct { char* word; int index; } data_t; typedef struct node node_t; typedef node { void *data; node_t *left; node_t…
0
votes
1 answer

Using command line in C to detect arguments, and then print out the first or second char of the arguments

I need to make a program that accepts no less than 2 and no more than 6 arguments at the command line and then prints out the 1st or 2nd character EX: asdf asdf asdf asdf prints out as: a s a s I have the initial array setup and working, the for…
Church
  • 115
  • 2
  • 3
  • 11
0
votes
2 answers

C Language: Newb translating code from javascript to C, code includes malloc, strncpy, pointers

I am converting (from javascript) a program that will take a string of variable length (but always under 100 char) and return the data contained in the string in individual variables. This is the first portion of my code, and obviously, I am new to…
user1486548
  • 1,201
  • 4
  • 15
  • 22
0
votes
3 answers

why will strcpy only copy a limited number of elements:

C++ newbie here. Writing a simple program. Everything works,except when I attempt to extract firstname and surname and print these individually, the number of letters printed in surname will always be the same size as the number in firstname. So if…
LegItJoe
  • 15
  • 2
  • 6