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
8
votes
2 answers

Crash upon delete through destructor

In the following program, i intend to copy char* line contents from one object to another through strcpy. However when the program ends, destructor of obj2 works fine but dtor of obj crashes. gdb shows different addresses of line for both…
anurag86
  • 1,635
  • 1
  • 16
  • 31
8
votes
5 answers

How to strcpy and return number of copied characters?

I want to copy a null-terminated string to another location and want to know how long the copied string was. Efficiency is of utmost importance. There ist the strcpy function which can achieve this, but it does not return how many characters are…
gexicide
  • 38,535
  • 21
  • 92
  • 152
8
votes
4 answers

Understanding char *, char[] and strcpy()

My understanding is as follows: char * points to a string constant, modifying the data it points to is undefined. You can, however, change where it points to. char[] refers to a block of memory that you can change. You can change its contents but…
CS Student
  • 1,613
  • 6
  • 24
  • 40
8
votes
7 answers

strcpy when dest buffer is smaller than src buffer

I am trying to understand the difference/disadvantages of strcpy and strncpy. Can somebody please help: void main() { char src[] = "this is a long string"; char dest[5]; strcpy(dest,src) ; printf("%s \n", dest); printf("%s \n", src); } The…
user193891
  • 103
  • 1
  • 1
  • 3
7
votes
6 answers

Valgrind Warning: Should I Take It Seriously

Background: I have a small routine that mimics fgets(character, 2, fp) except it takes a character from a string instead of a stream. newBuff is dynamically allocated string passed as a parameter and character is declared as char…
Chris Allen
  • 653
  • 4
  • 9
  • 17
6
votes
7 answers

strcpy... want to replace with strcpy_mine which will strncpy and null terminate

The clue is in the title but basically I've inherited some code which has 800+ instances of strcpy. I want to write a new function and then to replace strcpy with strcpy_mine. So I'm trying to work out what parameter list strcpy_mine will have. I…
timB33
  • 1,977
  • 16
  • 33
6
votes
2 answers

Using strcpy with a string array in C

I have a character array defined as follows: char *c[20]; I'm trying to do: strcpy(c[k], "undefined); but it's not working I've also tried defining it as char c[20][70] with no luck. Edit: I actually know it's an array of character arrays, I need it…
Adam
  • 61
  • 1
  • 1
  • 2
6
votes
5 answers

Why doesn't strcpy use a const pointer for dest?

Is there a reason strcpy's signature is this: char *strcpy(char *dest, const char *src); instead of this? char *strcpy(char *const dest, const char *src); As far as I know, the function will never change the pointer. Am I misunderstanding what…
Rothwell
  • 79
  • 4
6
votes
1 answer

Bus error (core dumped) when using strcpy to a mmap'ed file

I have a simple program going this: int main(void) { int fd; const char *text = "This is a test"; fd = open("/tmp/msyncTest", (O_CREAT | O_TRUNC | O_RDWR), (S_IRWXU | S_IRWXG | S_IRWXO) ); if ( fd < 0 ) { perror("open()…
HuangJie
  • 1,488
  • 1
  • 16
  • 33
6
votes
2 answers

How is strcpy implemented?

I have a question about using strcpy. I know the ANSI C standard says : source and destination must not overlap, otherwise the behaviour is unpredictable. I show you a piece of code that works as I expect if it is compiled using an old gnu C…
Nelu Cozac
  • 69
  • 1
  • 2
5
votes
4 answers

Segmentation fault around strcpy?

I know that you will rap me over the knuckles but. Why does it make Segmentation fault char* cmd; strcpy(cmd, argv[0]); when this doesn't char *cmd; cmd = "plop"; I didn't practice since a while, and can't remember why. ps: actually, i know that…
roro
  • 131
  • 1
  • 2
  • 8
5
votes
2 answers

What exactly is -fno-builtin doing here?

So I was reading Hacking the Art of Exploitation and in the book, they use the strcpy() function in their C code: 1 #include 2 #include 3 4 int main() { 5 char str_a[20]; 6 7 strcpy(str_a,…
user10199821
5
votes
3 answers

Why can't I do strcpy?

#include #include #include int main() { const char* hello = "Hello, World!"; char *str = malloc(14 * sizeof(char)); for (int i = 0; i < 14; i++) { strcpy(str[i],hello[i]); } str[14]='\0'; …
jhonnna
  • 107
  • 5
5
votes
5 answers

Cannot copy strings from pointer array with strcpy in C?

I am doing an exercise where a character pointer array is functioning as a way to store words. I do not understand why I cannot use 'strcpy' to copy the word 'hoi' to the second element of the array in the main function. When I compile the code I…
blabla444
  • 85
  • 5
5
votes
3 answers

How to convert current strcpy to strcpy_s?

I have large project having strcpy used everywhere. I am thinking to use strcpy_s instead of strcpy. I think almost 10,000 times I have used strcpy. It's so cumbersome to change every strcpy. Is there any efficient way for conversion?
sonu gupta
  • 473
  • 8
  • 21
1 2
3
63 64