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

C String copy,size of array

I'm trying to mimic the way strcpy works, by writing my own function. My question is more specific to the size of an array. I'm trying to copy contents of stringA into stringB, which I have declared as size of 1. Shouldn't I get an error saying…
hmmm
  • 93
  • 2
  • 9
2
votes
6 answers

How could I copy data that contain '\0' character

I'm trying to copy data that conatin '\0'. I'm using C++ . When the result of the research was negative, I decide to write my own fonction to copy data from one char* to another char*. But it doesn't return the wanted result ! My attempt is the…
xtensa1408
  • 584
  • 12
  • 32
2
votes
4 answers

Difference between using strcpy() and copying the address of a the char* in C

I have two dynamically allocated arrays. c char **a = (char**)malloc(sizeof(char*) * 5)); char **b = (char**)malloc(sizeof(char*) * 5)); for (int i = 0; i < 7, i++) { a[i] = (char*)malloc(sizeof(char)*7); b[i] =…
Kevin Cheng
  • 39
  • 1
  • 6
2
votes
4 answers

Strcpy Segmentation Fault C

I am learning some new things and get stuck on a simple strcpy operation. I don't understand why first time when I print works but second time it doesn't. #include #include #include int main() { char *name; …
user3623498
  • 69
  • 1
  • 1
  • 8
2
votes
4 answers

String in C and strcpy

I was learning the basics of C programming , and I wanted to test some lines for strings. This is my code: int main(){ char a[] = "abc"; strcpy(a,"pqrst"); printf("%s; %d",a, sizeof(a)); } I expected the code to output size=6 (p, q, r, s,…
2
votes
3 answers

strcpy() causes invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]

I'm trying to have an object named PReader (with a function named Execute(), taking no arguments) have a filename passed in through the constructor so Execute() can access it. The way I'm trying to set it up involves having the constructor allocate…
user3713712
  • 105
  • 1
  • 2
  • 6
2
votes
3 answers

Why doesn't strcpy work?

char sentence2[10]; strncpy(sentence2, second, sizeof(sentence2)); //shouldn't I specify the sizeof(source) instead of sizeof(destination)? sentence2[10] = '\0'; //Is this okay since strncpy does not provide the…
Mustafa
  • 177
  • 1
  • 2
  • 10
2
votes
4 answers

C++ not printing as expected with my own string copy function

sorry for another string copy question.. but I really can't find the reason why in my code, strTo can be printed, but strFinal can't in neither way. cout << strTo << endl; cout << strFinal << endl; while (*strFinal != '\0') { cout <<…
2
votes
5 answers

strcpy() implementation.. How to initialize a passing pointer to a function in the function itself

I'm trying to implement strcpy() standard function, as a function taking two pointers to characters and returning nothing. I've done the job right, but to avoid segmentation fault I had to initialize "dest" pointer in the main function before…
Salahuddin
  • 1,617
  • 3
  • 23
  • 37
2
votes
4 answers

Use the STRCPY C

I code: #include #include #include void main() { char string[]="stackflow"; strcpy(&string[0],&string[1]); puts(string); getch(); } The result is "tackflow". I don't understand about it. Who are you can…
Hoang
  • 55
  • 1
  • 2
  • 10
2
votes
2 answers

Regarding security flaw in strcpy

By causing buffer overflow we can overwrite the memory where flag value 0 is saved, so an even incorrect password will crack the code.. How will this happen internally? Could someone please explain in detail..how will this happen in memory…
codey modey
  • 983
  • 2
  • 10
  • 23
2
votes
3 answers

*char into main and *char into a struct

The two following codes are similar but the first has a structure, the second not. Why this code works (with no warnings)? #include #include struct prova { char *stringa; }; int main() { struct prova p; strcpy…
ᴜsᴇʀ
  • 1,109
  • 2
  • 9
  • 23
2
votes
3 answers

Copy a word from a function to a string with strcpy in C?

I'm in a basic C programming course and I'm trying to create a hangman game. I've been stuck with a problem for the last three hours and I'm not getting any wiser. Basically, I've created a function which reads a random line from a text file and…
JonteG
  • 23
  • 4
2
votes
1 answer

Run time error with .txt files loading using getline, strcpy, strtok c++

I'm having problem to load data from a .txt files. The problem is some times the code running perfectly but most of the time I just got an run time error. The funny thing is that I haven't made any changes to the code and there is some times the…
user3134453
  • 233
  • 2
  • 5
  • 14
2
votes
1 answer

Trouble using strcpy with character array and string values in Objective-C

I am taking a course in Objective-C and was told to "Create a character array called names. Populate the array with five first names. Use the strcpy command to copy the string values in to the array." I coded my solution to this but it keeps giving…