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
109
votes
10 answers

Why should you use strncpy instead of strcpy?

Edit: I've added the source for the example. I came across this example: char source[MAX] = "123456789"; char source1[MAX] = "123456789"; char destination[MAX] = "abcdefg"; char destination1[MAX] = "abcdefg"; char *return_string; int index = 5; /*…
Kredns
  • 36,461
  • 52
  • 152
  • 203
97
votes
9 answers

strcpy vs. memcpy

What is the difference between memcpy() and strcpy()? I tried to find it with the help of a program but both are giving the same output. int main() { char s[5] = { 's', 'a', '\0', 'c', 'h' }; char p[5]; char t[5]; strcpy(p, s); …
Sachin
  • 20,805
  • 32
  • 86
  • 99
90
votes
6 answers

strcpy vs strdup

I read that strcpy is for copying a string, and strdup returns a pointer to a new string to duplicate the string. Could you please explain what cases do you prefer to use strcpy and what cases do you prefer to use strdup?
johan
  • 1,943
  • 10
  • 31
  • 43
65
votes
6 answers

How can I correctly assign a new string value?

I'm trying to understand how to solve this trivial problem in C, in the cleanest/safest way. Here's my example: #include int main(int argc, char *argv[]) { typedef struct { char name[20]; char surname[20]; …
Gianluca Bargelli
  • 1,780
  • 2
  • 17
  • 23
58
votes
7 answers

Proper way to empty a C-String

I've been working on a project in C that requires me to mess around with strings a lot. Normally, I do program in C++, so this is a bit different than just saying string.empty(). I'm wondering what would be the proper way to empty a string in C. …
Benjamin
  • 1,223
  • 1
  • 13
  • 22
57
votes
7 answers

Does C have a string type?

I have recently started programming in C, coming from Java and Python. Now, in my book I have noticed that to make a "Hello World" program, the syntax is something like this: char message[10] strcpy(message, "Hello, world!") printf("%s\n",…
rel-s
  • 6,108
  • 11
  • 38
  • 50
56
votes
8 answers

error: function returns address of local variable

I'm beginner with C and I am learning on my own. I am creating the following function: char *foo(int x){ if(x < 0){ char a[1000]; char b = "blah"; x = x - 1; char *c = foo(x); strcpy(a, b); …
Hello World
  • 1,273
  • 2
  • 10
  • 8
40
votes
6 answers

strcpy() return value

A lot of the functions from the standard C library, especially the ones for string manipulation, and most notably strcpy(), share the following prototype: char *the_function (char *destination, ...) The return value of these functions is in fact…
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
37
votes
4 answers

C - why is strcpy() necessary

Can someone please explain to me why strcpy() is necessary to assign strings to character arrays, such as in the following code snippet. int main(void) { char s[4]; s = "abc"; //Fails strcpy(s, "abc"); //Succeeds return 0; } What is the reason…
C_p678
  • 437
  • 1
  • 5
  • 5
34
votes
2 answers

Difference between 'strcpy' and 'strcpy_s'?

When i tries to use strcpy to copy a string it gave me a compile error. error C4996 'strcpy': This function or variable may be unsafe. Consider using `strcpy_s` instead. To disable deprecation, use `_CRT_SECURE_NO_WARNINGS`. See online help for…
Bluebaby
  • 361
  • 1
  • 3
  • 4
32
votes
6 answers

strcpy()/strncpy() crashes on structure member with extra space when optimization is turned on on Unix?

When writing a project, I ran into a strange issue. This is the minimal code I managed to write to recreate the issue. I am intentionally storing an actual string in the place of something else, with enough space allocated. // #include…
iBug
  • 35,554
  • 7
  • 89
  • 134
23
votes
3 answers

segmentation fault with strcpy

I am wondering why am I getting segmentation fault in the below code. int main(void) { char str[100]="My name is Vutukuri"; char *str_old,*str_new; str_old=str; strcpy(str_new,str_old); puts(str_new); …
Teja
  • 13,214
  • 36
  • 93
  • 155
22
votes
7 answers

Converting char* to unsigned char*

How do I copy a char* to a unsigned char* correctly in C. Following is my code int main(int argc, char **argv) { unsigned char *digest; digest = malloc(20 * sizeof(unsigned char)); strncpy(digest, argv[2], 20); return 0; } I would…
Rajiv
  • 545
  • 1
  • 6
  • 12
22
votes
17 answers

C strcpy() - evil?

Some people seem to think that C's strcpy() function is bad or evil. While I admit that it's usually better to use strncpy() in order to avoid buffer overflows, the following (an implementation of the strdup() function for those not lucky enough to…
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
22
votes
2 answers

Is memcpy() usually faster than strcpy()?

Is memcpy() usually faster than strcpy() (on most real platforms)? (I assume that size of the string is known.) If I remember i386 assembler correctly, there are loop instructions which copy a given number of bytes or words. So it is the fastest…
porton
  • 5,214
  • 11
  • 47
  • 95
1
2 3
63 64