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

how to change a character array in typedef struct from a function?

i'm trying to change array a inside set of typedef through function readset but it doesn't do anything to it and im trying to understand how to use a pointer so i would just send it to the function but its not working how to do that? #include…
AdmDTR
  • 35
  • 1
  • 9
0
votes
1 answer

Adressing certain array location in C

I'm trying to learn C, so while making some exercises I ran into a problem I can't understand. I created a character array buf [100][100], in it I store some strings. Now i would like to change a certain string in to another one by using the strcpy…
Sam
  • 1
  • 1
0
votes
2 answers

get segmentation fault error when use strcpy function

I am implementing the doubly linked list, in functions create_head_node, insert_head, insert_tail i have to copy the array name to node. Then I use function strcpy to copy it but I got segmentation fault error. I reimplement function strcpy by…
Becker
  • 147
  • 7
0
votes
6 answers

Strcpy behavior with stack array c++

Here is my program : #include const int SIZE =10; int main() { char aName [SIZE]; // creates an array on the stack std::strcpy(aName, "Mary"); return 0; } This program is obviously useless, I am just trying to understand…
CSstudZ
  • 101
  • 10
0
votes
3 answers

Why strcpy_s is safer than strcpy?

When I am trying to use the strcpy function the visual studio gives me an 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…
Osama El-Ghonimy
  • 335
  • 2
  • 12
0
votes
0 answers

Why does strcpy specifically requires an array as first argument? (As opposed to a char* const ptr)

Isn't an array basically a const pointer to char? #include #include void f(char* s) { strcpy(s,"ciao"); } void main() { char str[10]="mamma"; char * const ptr="papa"; f(str); // <-- works as expected …
S E
  • 113
  • 1
  • 7
0
votes
2 answers

Use of strcpy() in pointers to string in C

I'm trying to use strcpy() with pointers to strings and after a successful compilation when I run it it gives error. I don't know why this is happening. int main() { char *s1="abcd"; char *s2="efgh"; …
0
votes
1 answer

Correct use of strcpy() and avoid read and write size errors in valgrind

I have this code: static void foo(char *string1, char *string2) { char *string1_copy= malloc(strlen(string1)); strcpy(string1_copy, haystack); char *string2_copy = malloc(strlen(string2)); strcpy(string2_copy, needle); } I have…
cheshire
  • 1,109
  • 3
  • 15
  • 37
0
votes
1 answer

Strcpy() not copying the second character in a string

I have been running into issues with the strcpy() function in C. In this function I take a string in buffer and the string contains a something along the lines of '(213);' and I am trying to remove the brackets so the output would be something like…
ksss
  • 35
  • 5
0
votes
1 answer

Printed output differs when printing variables of function

I have tried solving an exercise where we have to return a struct containing the first whitespace-separated word and its length of a given string. Example: "Test string" returns {"Test", 4}. To solve this problem I have implemented the following…
pr0f3ss
  • 527
  • 1
  • 4
  • 17
0
votes
1 answer

How to compare string in if statement?

I am a beginner in C++ and I was trying to make a decision function which I could use in future programs. The idea is that the program would ask a yes/no question, the user would type in yes or no and they would receive an output according to their…
0
votes
2 answers

C: Segfault caused by assigning string to struct array using strcpy

I am reading in a text file containing 10 rows and 3 columns. Each element needs to be put into an array for further manipulation. The fileToArray method will single out each string and i can successfully print each string individually however I…
0
votes
0 answers

strcpy Gives Segmentation fault

The following is a recursive function that is supposed to turn an integer into a string char* int_to_word(int word_int){ static char new[2]; char alphabet[26]={"abcdefghijklmnopqrstuvwxyz"}; new[0]=alphabet[word_int%27-1]; …
Ben R.
  • 7
  • 2
0
votes
2 answers

What i should use: strcpy or pointers?

typedef struct node{ char *name; //etc... }N If i had char *string got from stdin and i wanted to pass it to N, i could do easily N->name=string, or it would be bettere to use strcpy? which is more correct? which is more efficient? why?
thesmizzle
  • 21
  • 4
0
votes
0 answers

Why is qsort seg faulting?

I'm trying to compare two .txt files line-by-line. From the two files, I want to print a third .txt file with all the strings of the first two files, without any repeats and in alphabetical order. When I try running the code I get a segmentation…
M_G
  • 1
  • 2