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
1 answer

printf executes after a seg-fault in strcpy?

I have the following code: char buf[10]; strcpy(buf, "This is a string longer than way longer than ten characters."); printf("%s\n", buf); I know that the second line will result in a segfault as I'm writing past the length of the array…
tj56
  • 162
  • 3
  • 12
-2
votes
1 answer

strcpy() alternative for integers and doubles - c

So I used this code in order to pass a variable in to my struct: strcpy(s[i].orderName, Name); and it works how I want it to. However, the rest of my variables are integers and a double and it appears there is no "intcpy()" alternative from what I…
Ibrahim
  • 75
  • 7
-2
votes
2 answers

C - Segmentation fault at strcpy()

#include #include #include char *vStrs[] = {"Max", "Moritz", "Bolte", "Hans Huckebein", "Helene", "Antonius", "Boeck", "Maecke", "Lempel", "Schlich"}; int main() { int num = sizeof(vStrs) / sizeof(vStrs[0]); …
rchrdmgwtz
  • 55
  • 2
-2
votes
3 answers

String duplicates when printed to text file

I'm trying to write a sentence string to a text file, but it duplicates or something when I do so. Why is this, and how do I get it to only print once? Program excerpt: FILE *memberAdd; typedef struct { char id[5], name[100]; } Member; Member…
lefrost
  • 461
  • 5
  • 17
-2
votes
1 answer

strcpy() always miss some characters

I'm working on a project using UDP protocol to transfer a file, but when I use strcpy() to copy a buffer into another string, it always missing some characters. The simple idea is that: I defined a struct: struct frame{ int…
user419050
  • 41
  • 2
  • 6
-2
votes
1 answer

Passing a char array as parameter in method, C language

this is my first C language coding. It is for my coursework and I have some troubles. I want to pass a char[] as a parameter to a method I want to copy this char[] to another char[]. For this I am using strcpy. So when I do: main(){ char asd[20] =…
-2
votes
1 answer

Segmentation fault (core dumped) with the strcpy-function in c

I am trying to write a function which will read two strings stringArray[MAX]="ABADDFDEFBFCCHCGGEHJJI" and popArr[MAX]="ABCDEFGHIJ" and generate an output like…
Blind0ne
  • 1,015
  • 12
  • 28
-2
votes
2 answers

strcpy seg fault in C

Curious about what is going wrong with this strcpy. int main(void){ char *history[10]; for(int i = 0; i < 10; i++){ history[i] = NULL; } char line[80]; fgets(line,80,stdin); strcpy(history[0],line); //This line…
MoStack
  • 29
  • 2
  • 3
-2
votes
2 answers

Seg fault occurs when copying char pointer into array of char pointers

I am reading in a line of text from a file, and trying to split it on spaces with strtok(), then storing each token in an array of char pointers using strcpy(), within a function called tokenize. Code: /** ** Tokenizes contents of buffer into…
Ungeheuer
  • 1,393
  • 3
  • 17
  • 32
-2
votes
1 answer

String pointers and strcpy

Why does this program give segmentation fault int main() { char *ptr; ptr = (char *)malloc(15*sizeof(char)); ptr = "string"; strcpy(ptr,"NewString"); } while this does not int main() { char *ptr; ptr = (char…
Kranthi
  • 1
  • 1
-2
votes
1 answer

Replacements for deprecated strcpy/strcat when using char*

I have code for a 'zombie translator' as an example from my professor. From what I can tell it takes a string of english words and applies a few rules to it via functions. It currently uses strcpy and strcat to do this, however it will not compile…
lana
  • 33
  • 6
-2
votes
1 answer

strcpy is copying sth character onwards i string . How to resolve this error?

My complete code is at pastebin. There is a train database , and user enters train number to book a ticket.The function updt_tick should copy the values of train's name,source and destination into passenger's reservation object. But the problem is…
Abhishek Ranjan
  • 498
  • 3
  • 17
-2
votes
1 answer

Usage of strcpy with pointers to characters

Why does this code crash? Is strcpy not being used properly?
-2
votes
1 answer

Safe way to concat two strings in C

I have the following code that concats two strings: char *getConcatString(char *str1, char *str2) { char *finalString = malloc(1 + strlen(str1) + strlen(str2)); // Needs to be freed by the user after use if(finalString == NULL) …
user3266083
  • 103
  • 3
  • 12
-2
votes
2 answers

C : integer variables randomly change values

I just started learning the C language. I have a good history with C# and Java though. #include #include #include "info.h" int main() { int day = 24, month = 3, year = 2016; char name[] = "Ahmad\0"; …
John Smith
  • 25
  • 4