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
11 answers

Can you change the size of what a pointer point to

For example if a pointer points to an array of chars that read "Hello how are you?" And you only want the pointer to point to Hello. I am passing in a char pointer and when I cout it, it reads the entire array. I try to cut down the size using a…
Aaron
  • 4,380
  • 19
  • 85
  • 141
-1
votes
0 answers

error: ‘strncpy_s’ was not declared in this scope

#define __STDC_WANT_LIB_EXT1__ 1 #define __STDC_LIB_EXT1__ 1 #include #include #include #include int main() { // Write C code here printf("Hello world"); char* key=""; char* key1=""; char…
Ek1234
  • 407
  • 3
  • 7
  • 17
-1
votes
1 answer

Copying a subarray from an array of substrings in c

I'm working on a function which is supposed to take an array of elements defined by a struct. typedef struct word{ char word[100]; double weight; } word; The function takes in a pointer, word *words and creates a subarray which includes…
shari
  • 33
  • 3
-1
votes
1 answer

Issue with string length 76

I am at a loss here. Will post my code in a short while...just that its too long to extract portions of the "troubling" giving code. Will expalin my issue here: I store a string(path to a file or directory) in an array of structure { char *path;…
Lipika Deka
  • 3,774
  • 6
  • 43
  • 56
-1
votes
4 answers

The loop of the strncpy function is not understood

char *strncpy(char *dest, const char *source, size_t n) { char *start = dest; while (n && (*dest++ = *source++)) n--; // I don't understand from the code below this // I know I put'\0' at the end of dest. // But I don't know how if (n) while…
junto
  • 13
  • 1
-1
votes
2 answers

third argument of the strncpy changes my local variable

char activeip[11]="0123456789"; char buffer[1001]; int MAX_SIZE = 1000; printf("MAX_SIZE %d\n", MAX_SIZE); strncpy(buffer, "string here....... ",MAX_SIZE+1); printf("MAX_SIZE %d\n", MAX_SIZE); strncpy(&buffer[strlen(buffer)],activeip,MAX_SIZE+1…
-1
votes
2 answers

How to convert my string into array of chars

Here is a problem. When I try to convert it by using strncpy_s, array has some type of "trash data" from memory in the end of it. Even when I fill buffer with "\0". How to convert it clear? typedef class Ryadok { private: int LengthOf = 0; …
-1
votes
2 answers

I wanna make strncpy function directly, can you review my code? There is bus Error

I wanna Make strncpy function by code, not by using Library or Header but There is zsh bus error..... What's wrong with my code? What's the zsh bus error?? #include #include char *ft_strncpy(char *dest, char *src, unsigned…
Joseph
  • 17
  • 1
  • 4
-1
votes
1 answer

C - strncpy segfaults when using pointer

I've got the following piece of code: #include #include int main(void) { char *src = "This is my string."; char *dest, *ret; //char dest[64], *ret; ret = strncpy(dest, src, 5); size_t s = strlen(ret); …
HTF
  • 6,632
  • 6
  • 30
  • 49
-1
votes
2 answers

I have encountered "Buffer overflow-array index out of bounds" error

I do understand the error but don't know what is the proper solution for this. It says Array 'filename' of size 512 may use index value(s) MIN..1022 Is it because strncat is called right after strncpy? I tried doing strlen, but that introduced…
-1
votes
3 answers

Why is vsnprintf Not Writing the Same Number of Characters as strncpy Would?

I've asked the same question about strncpy, but there the string ends up containing the whole input string. When passing a string to vsnprintf the last character always gets chopped off: https://rextester.com/UIQMX91570 For simplicity I've also…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
-1
votes
1 answer

strncpy char string issue when deleting the array

I don't know, problem with this error. However, I think I should delete uName[] before I delete the []cpp. How can I do for this? #define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; class cppUr { public: …
-1
votes
2 answers

After calling strcpy, the second char array becomes empty

Here is the function prototype in my program: void FindRepStr(char str[], const char findStr[], const char replaceStr[]); It find the findStr[] in str[] and replace it with replaceStr[]. Here is my code: void FindRepStr(char str[], const char…
BobHU
  • 402
  • 5
  • 18
-1
votes
2 answers

Why didn't work C++ code(strncpy_s)?

I'm C++ beginner. When I have studied Class of C++, this example code did not work. This code is interrupted by strncpy_s() func in NameCard class constructor. I tried debugging, but I could not find the cause. Could you help me? This is full…
zer0day
  • 13
  • 3
-1
votes
1 answer

In C, I'm unable to copy a single element of an array of character strings to another string

I have a string "prompt" that holds a string of characters. The user must be able to change the prompt with user input, which I've collected and separated (using whitespace as a delimiter) into an array of char strings (tokens). For example,…