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

memory error using valgrind, strcpy

I've been using valgrind, but for some reason I keep getting a memory error using a simple string copy with two strings that are the same size, in C. The Code effected is: node->entry =…
Cail Demetri
  • 2,138
  • 3
  • 22
  • 24
3
votes
4 answers

Why does a char array need strcpy and char star doesn't - using structs in C

I have a misunderstanding regarding this code - typedef struct _EXP{ int x; char* name; char lastName[40]; }XMP ...main... XMP a; a.name = "eaaa"; a.lastName = strcpy(a.lastName, "bbb"); Why can't I use: a.lastName = "bbbb"; and…
user1386966
  • 3,302
  • 13
  • 43
  • 72
2
votes
2 answers

'strcpy' : cannot convert parameter 2 from 'WCHAR *' to 'const char *

Having some issues with strcpy... Getting this error: strcpy' : cannot convert parameter 2 from 'WCHAR *' to 'const char * Here is the code... char FunctionName[ 256 ]; UFunction *pUFunc = NULL; strcpy( FunctionName,…
E3pO
  • 493
  • 1
  • 9
  • 21
2
votes
2 answers

string copy() function return misunderstood? (C)

(EDIT) Probable misunderstanding in updating and returning a string from a function in C. I'm a student and have just started trying out dynamic memory allocation techniques. Recently, I was told to work on a string copy() function similar to the…
IICN-08
  • 23
  • 5
2
votes
3 answers

Is it possible to create a character array within a strcpy?

Is it possible to copy a string using C's strcpy without first assigning memory to a char *character_array? Basically, is something like this possible: strcpy(char *my_string, another_string); where my_string becomes an initialised string with the…
Connor
  • 867
  • 7
  • 18
2
votes
7 answers

invalid conversion from 'char' to 'char*' using strcpy

Ok so here are the parts of my code that I'm having trouble with: char * historyArray; historyArray = new char [20]; //get input cin.getline(readBuffer, 512); cout << readBuffer < 0; i--){ …
OmegaTwig
  • 243
  • 1
  • 4
  • 15
2
votes
2 answers

I have problem understanding the condition of the following while loop

void strcpy(char *s, char *t) { int i; i = 0; while ((*s = *t) != '\0') { s++; t++; } } I made a function to copy string t to string s using pointers from K&R. The while loop uses (*s = *t)!='\0' which is supposed to…
Zolo_Ryan
  • 51
  • 6
2
votes
2 answers

Why one variable can not be read after swaping two variables

In the output, there is only water is printed, but no soda is printed. But in my opinion, soda has less size, it should be easily saved in x because x has a bigger size. If I set y who has more chars than x, then it doesn't have such a…
Elias
  • 179
  • 8
2
votes
1 answer

saving file in arrays without malloc with c

I have a homework where i need to read from a file and save in arrays informations like the city name, the country code, the county name and the population #include #include int main () { struct Country { char…
mr ma
  • 21
  • 2
2
votes
2 answers

Pointer seems not be sync with printf's output

I have trouble understanding what pointer2 contains. The second printf prints llo World, but the third one prints Hey you guys!. Why would it be like that if strcpy copies y you guys!\n into llo World. From my understanding of the below program the…
2
votes
1 answer

strcpy crashing program

I creating a program which imports date and time entries (eg. 29/11/2021-15:33:56) from a file and stores them in an array. We are asked to then create a function (timeStampToSeconds) to convert these to long ints. Once we have the long ints we…
Cik02
  • 59
  • 6
2
votes
2 answers

is it possible to use strcpy with single character variables?

here is the main function in a program that i wrote in which i need to sort an array of characters, making the ones with an even ascii code at the beginning, and i wanna display how the array is sorted at every…
2
votes
3 answers

Why is this use of strcpy considered bad?

I've spotted the following piece of C code, marked as BAD (aka buffer overflow bad). The problem is I don't quite get why? The input string length is captured before the allocation etc. char *my_strdup(const char *s) { size_t len = strlen(s) +…
DegH
  • 53
  • 8
2
votes
1 answer

No output produce after running the code. Blank and got nothing. Why?

No output produce after running the code. Blank and got nothing. What is the problem ? #include #include char* foo(){ char temp[] = "World"; char *result; strcpy(result, temp); return result; } int main(){ …
emperor_c
  • 99
  • 2
  • 9
2
votes
0 answers

I got a Bus error while using strcpy() but memcpy() is not

Consider the following code, I compile and run on aarch64: struct test_msg { uint64_t seq; char data[4096]; }; static inline void serialize(void *dest, const struct test_msg *src) { memcpy(dest, &src->seq, sizeof(src->seq)); …
Minh Do
  • 43
  • 7