Questions tagged [strcpy]

The C standard library function, "strcpy()," is used to copy non-overlapping, null-terminated strings. It is also defined as "std::strcpy()" in the C++ standard library.

Used to copy null-terminated, non-overlapping strings, it is defined in the <string.h> standard header. It is also defined in the C++ standard library in the <cstring> header.

Documentation for C strcpy.

Documentation for C++ std::strcpy.

947 questions
3
votes
2 answers

Why do i gett warnings by using strcpy_s and strcat_s?

I programmed an example on my Win64 PC with code:blocks using strcpy_s and strcat_s. The program works, but I get the warning messages "warning: implicit declaration of function 'strcpy_s' for strc_s and strcat_s respectively. The compiler settings…
Heimo
  • 39
  • 2
3
votes
3 answers

Why does strncpy() produce garbage when the dest is smaller than the src but large enough to fit the wanted substring of src?

I tried to limit the number of n bytes copied to dest (here str1) by using strncpy(). The dest is big enough for the n bytes, but the output produced garbage when dest is smaller than the source (here argv[1]). This looks different, when i make dest…
Coder
  • 197
  • 6
3
votes
3 answers

Updating the string using function in c

I have written following c code to update the char array rb but it is printing garbage value #include void update(char* buff){ char word[] = "HII_O"; buff = word; return; } int main(){ char rb[6]; update(rb); rb[5]…
3
votes
1 answer

C6387 for memcpy, strcpy and strcpy_s

It seems that I cannot shake the C6387 warning. typedef struct HashBind{ char* cKeyIdentifier; void* vValue; } HashBind; .... HashBind* strNewBind = malloc(sizeof(HashBind)); strNewBind -> cKeyIdentifier = (char*)…
user10817807
3
votes
6 answers

How to properly implement strcpy in c?

According to this: strcpy vs strdup, strcpy could be implemented with a loop, they used this while(*ptr2++ = *ptr1++). I have tried to do similar: #include #include int main(){ char *des = malloc(10); for(char…
milanHrabos
  • 2,010
  • 3
  • 11
  • 45
3
votes
2 answers

realloc memory in string array in C

I'm trying to satisfy valgrind and come up with a nice implementation, but I'm coming across a snag. Essentially what I'm trying to do is reduce two strings in an array to one. Let's say arr contains { "One", "Two", "Three" } And that the memory…
Rio
  • 14,182
  • 21
  • 67
  • 107
3
votes
5 answers

C - memcpy with char * with length greater than source string length

I have the following code in C now int length = 50 char *target_str = (char*) malloc(length); char *source_str = read_string_from_somewhere() // read a string from somewhere // with length, say…
abisheksampath
  • 376
  • 8
  • 23
3
votes
2 answers

What defines strscpy in C?

From this video on youtube by Kees Cook from linux.conf.au 2019 he mentions strscpy as being preferred but generally doing what users want (less NUL-padding). However, he doesn't say what defines this (spec or header), Slide from the video, I…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
3
votes
6 answers

Do strncpy/memcpy/memmove copy the data byte by byte or in another efficiently way?

As we know, in a multi-bytes word computer such as x86/x86_64, it is more efficiently to copy/move a big bulk of memory word by word (4 or 8 bytes per step), than to do so byte by byte. I'm curious about which way would strncpy/memcpy/memmove do…
Leon
  • 1,489
  • 1
  • 12
  • 31
3
votes
3 answers

how to use strncpy correctly?

I know strncpy is a safer version of strcpy as said here. However, when I want to copy from src to dst and dst is not a clean buffer, I get unwanted results, which can be avoided by strcpy: char *p = "123"; char a[10] =…
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
3
votes
1 answer

Why does C's strcpy fail with doubly indexed arrays?

The following code seems to segfault and I cannot figure out why. #include static char src[] = "aaa"; int main() { char* target[2] = {"cccc","bbbbbbbbbb"}; strcpy(target[1],src); return 0; }
3
votes
3 answers

C: Writing 4 bytes into a region of size 3 overflows the destination?

My simple C program is as follows. Initially, I've defined variable buf1 with 3 char. I don't have any problem with 2 char such as AB or XY user@linux:~/c# cat buff.c; gcc buff.c -o buff; echo -e '\n'; ./buff #include #include…
user9013730
3
votes
3 answers

About strcpy() in TCPL

I am reading The C Programming Language, and when it gets to Character Pointers and Functions (5.5) I get a problem. In 5.5, the authors showed four versions of strcpy(). My problem lies in the version 3: /*strcpy: copy t to s; pointer version…
1MinLeft
  • 93
  • 7
3
votes
1 answer

char pointers and strcpy in C

First let me start saying I have read all the questions related to this subject and can't find a solution for my problem. All the answers seem to be to stop using pointers and chars(which I need to do) or are about other structures. I need to…
M.O.
  • 476
  • 7
  • 19
3
votes
3 answers

strcpy() and arrays of strings

I need to store the input from a user into an array of strings. #include #include #include char *history[10] = {0}; int main (void) { char input[256]; input = "input"; strcpy(history[0], input); …
Spencer
  • 4,018
  • 10
  • 33
  • 43