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

strcpy_s not working with gcc

I have a C++11 project, and I added some strcpy_s method calls. This works on windows, but when compiling on gcc, there is an error stating that strcpy_s symbol is not found. I did add the line #define __STDC_WANT_LIB_EXT1__ 1 to the code, to no…
Jacko
  • 12,665
  • 18
  • 75
  • 126
17
votes
6 answers

'strcpy' with 'malloc'?

Is it safe to do something like the following? #include #include #include int main(void) { char* msg; strcpy(msg, "Hello World!!!"); //<--------- printf("%s\n", msg); return 0; } Or should the…
LEH
  • 211
  • 1
  • 2
  • 5
15
votes
5 answers

Does a string created with 'strcpy' need to be freed?

Does a string created with 'strcpy' need to be freed? And how to free it? Edit: The destination is allocated like this: char* buffer[LEN];
Kristina
  • 15,859
  • 29
  • 111
  • 181
15
votes
4 answers

Alternative of strcpy in c++

In C i used strcpy to make a deep copy of a string, but is it still 'fine' to use strcpy in C++ or are there better alternatives which i should use instead ?
Aerus
  • 4,332
  • 5
  • 43
  • 62
13
votes
1 answer

Copy unsigned char array

What would be the best way to copy unsigned char array to another? For example: unsigned char q[1000]; unsigned char p[1000]; strcpy (q,&p); The above code does not work, it gives me error saying "cannot convert parameter 1 from unsigned char…
user2829
  • 451
  • 1
  • 7
  • 21
12
votes
9 answers

How to fix strcpy so that it detects overlapping strings

In an interview, I was asked to write an implementation of strcpy and then fix it so that it properly handles overlapping strings. My implementation is below and it is very naive. How do I fix it so that: It detects overlapping strings and after…
user7
  • 2,339
  • 5
  • 25
  • 29
11
votes
5 answers

Why does this intentionally incorrect use of strcpy not fail horribly?

Why does the below C code using strcpy work just fine for me? I tried to make it fail in two ways: 1) I tried strcpy from a string literal into allocated memory that was too small to contain it. It copied the whole thing and didn't complain. 2) I…
Gabe Durazo
  • 1,789
  • 2
  • 19
  • 27
11
votes
4 answers

How does strcpy_s work?

As we all know, strcpy_s is a safety version of strcpy. But I wonder how it works ... let's see some examples. strpy_s's declaration: errno_t strcpy_s(_CHAR *_DEST, size_t _SIZE, const _CHAR *_SRC) eg1 char dest[5]; char* src =…
Joey
  • 228
  • 1
  • 2
  • 11
10
votes
4 answers

what does the "const void*" mean in memmove?

The second arg in the prototypes for memmove/memcpy/strcpy are similar: For example: void *memmove(void *dest, const void *src, size_t n); //const void* char *strcpy(char *dest, const char *src); //const char* But apparently, if dest and src…
Alcott
  • 17,905
  • 32
  • 116
  • 173
10
votes
9 answers

Why no sanity checks in legacy strcpy()

Following is the most popular implementation of strcpy in traditional systems. Why dest and src are not checked for NULL in the start? I heard once that in old days the memory was limited so short code was always preferred. Will you implement strcpy…
user436748
  • 109
  • 1
  • 4
10
votes
3 answers

Why is strcpy unsafe in C?

I am a beginner, and I am learning how to copy a string in C now. Here is a problem I just met: Every time I try to use "strcpy" command to copy from string 1 to string 2, Visual Studio 2013 will give me an error/warning message saying that "strcpy"…
MechAvia
  • 343
  • 1
  • 4
  • 13
10
votes
9 answers

Why must a pointer to a char array need strcpy to assign characters to its array and double quotes assignment will not work?

The first example does not work when you go to delete the pointer. The program either hangs when I add the null terminator or without it I get: Debug Assertion Failed Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) from Visual Studio 2008 //Won't…
Omar
  • 750
  • 3
  • 7
  • 13
9
votes
6 answers

Program aborts when using strcpy on a char pointer? (Works fine on char array)

I'm perplexed as to why the following doesn't work: char * f = "abcdef"; strcpy(f, "abcdef"); printf("%s",f); char s[] = "ddd"; strcpy(&s[0], "eee"); printf("%s", s); In both examples strcpy received a char * yet on the first example it dies a…
John Smith
  • 211
  • 1
  • 3
  • 4
9
votes
5 answers

Appending element into an array of strings in C

I have an array of strings with a given size, without using any memory allocation, how do I append something into it? Say I run the code, its waiting for something you want to enter, you enter "bond", how do I append this into an array ? A[10] ?
dave_1234
  • 147
  • 1
  • 3
  • 10
8
votes
4 answers

C++ copy std::string to char array with no null termination

I am writing to a binary file using a struct that just contains a char[32]. I basically need to format each block of data by performing various calculations on string arrays and concatenating the results. I am attempting to copy an std::string to a…
Geo Ego
  • 1,315
  • 7
  • 27
  • 53
1
2
3
63 64