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

C: What size should you allocate to a string array to be passed to strcpy, to be copied into.?

If I need to copy a string, src into the array, dest, using strcpy or strncpy, should I allocate an arbitrarily large sized array (like char dest[1024] for example) or should I calculate the size of src using something like strlen and then…
First User
  • 704
  • 5
  • 12
2
votes
2 answers

What should be the right usage of strncpy_s() - secure string copy - to handle all the possible corner cases?

I got to know about the function - strncpy_s(), which is called the secure version of string copy. More secure than the functions - strcpy() and strncpy(). strncpy_s() is described more at this link…
Darshan L
  • 824
  • 8
  • 30
2
votes
2 answers

How strcpy changes the value of string

I got this homework to decide what the following code will do (on paper, without testing on computer). char s1[]="Short Message Service", *s2, *s3; s2=strchr(s1,'M'); s3=strchr(s2,'S'); strncpy(s1+1,s2,1); strcpy(s1+2,s3); When I wanted to check…
Blu Dog
  • 31
  • 5
2
votes
3 answers

How can I copy multiple char* strings into one using strcpy_s()?

Using the strcpy_s() function I want to collate the first three strings into the final one to print my full name. This is what I have and it doesn't work as I'm using char* strings and not std::strings. #include using namespace std; int…
2
votes
4 answers

Do not understand about strcpy in C++

char* s1; strcpy(s1,"smilehihi"); s1[6] = 'a'; When I compile, VS do not have any errors. But in the runtime, my code makes mistake. I think I do not really understand about strcpy
2
votes
1 answer

C function strcpy() randomly change one character

I'm learning string functions in C and I can't seem to get my teacher's demo using strcpy() to remove white spaces to work on my machine while it works perfectly fine for him. Here's the source code: #include #include int…
Tai Nguyen
  • 31
  • 1
  • 6
2
votes
2 answers

How to concatenate string and int in C without using sprintf?

I am writing the following code for an embedded system, where I am putting up strings and integers into a buffer. There are several sprintfs in the code similar to the one below (with more than one strings and integers). sprintf(txbuf,…
Annie
  • 21
  • 5
2
votes
3 answers

Modifying a String (char array)

I am trying to modify a string in C language char signal_cat[8]; if (k == 1) { strcpy_s(signal_cat, "HPHA",6); //why cant I change char array (string) values??? } else if (k == 2) { …
WXYZ
  • 53
  • 2
  • 9
2
votes
2 answers

Value of char array changing, upon calling strcpy on a different char array

When experimenting ways to copy strings to a C style array, due to some weird limitations I stumbled upon this weird behavior, firstly trying to copy the string with std::string::copy() seems to work fine until I'm trying to copy the same string…
AlexG
  • 5,649
  • 6
  • 26
  • 43
2
votes
4 answers

Using strcpy_s to copy string to char*

I know that when you use strcpy_s you are supposed to supply the size of the destination string as the second parameter. But if the destination string is a char* then I'm not sure I'm doing it right. I have three examples: char* dest = new char; //…
Kurt Dawson
  • 29
  • 1
  • 3
2
votes
2 answers

How to assign string of one variable to other variable?

This is my first question on this site. How do i assign string of one variable to other variable. What am i doing wrong here? #include #include main(){ char a[30],b[30]; scanf("%s",a); b[30]=a[30]; printf("%s",b); }
2
votes
1 answer

Fixing AddressSanitizer: strcpy-param-overlap with memmove?

I am poking around in an old & quite buggy C program. When compiled with gcc -fsanitize=address I got this error while running the program itself: ==635==ERROR: AddressSanitizer: strcpy-param-overlap: memory ranges [0x7f37e8cfd5b5,0x7f37e8cfd5b8)…
darked89
  • 332
  • 1
  • 2
  • 17
2
votes
1 answer

Compilation error appears because of missing const?

I am trying to run a code that defines objects that are a collection of English letters. I dont know why it does not compile. I have tried to change from int to const int but it is not the case, and also added the disable 4996 message but it…
2
votes
1 answer

How and why exactly does char* s2 change after the last strcpy() call?

This is one of the questions that may end up on a test I'm going to have in a couple of days at school. I'm having trouble with understanding how char* s2 changes from "Message Service" to "ice" after the last strcpy() call. I think that this is…
2
votes
5 answers

Why does strcpy omit the first character in source string when using destination string of size 1?

In C the strcpy function is used to copy a source into a destination string. But when I use a destination char array of size 1 the strcpy correctly copies the source into the destination. But it also changes the source char array. I want to…
FlarrowVerse
  • 197
  • 2
  • 13