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:string getting corrupted between malloc and strcpy

I am very confused why this is happening though I've used such statements well before. I am copying one string into other simply and my original string is getting corrupted, even before strtok gets called. This is the relevant code snippet, after…
Diwakar Sharma
  • 415
  • 1
  • 9
  • 26
2
votes
1 answer

Buffer Overflow Test Program Not Behaving as Expected

I wrote the following program to teach myself about buffer overflow and memory registers: #include int main(int argc, char *argv[]) { char name[400]; strcpy(name,argv[1]); printf("Hello %s!\n",name); return 0; } It takes…
735Tesla
  • 3,162
  • 4
  • 34
  • 57
2
votes
1 answer

string type in strcpy() in C++

#include #include using namespace std; int main() { char a[10]; string b = "Hello"; char c[] = "Hello"; char *d = "Hello"; strcpy(a, b); // compiler complains. strcpy(a, c); strcpy(a, d); } I know…
cyberion
  • 41
  • 4
2
votes
2 answers

C++ strcpy pointers that are passed to a function

I am passing an string or a char array to a function and swapping them but losing the first char array's value for some reason. Here is my code: void foo(char* a, char* b){ char* temp; temp = new char[strlen(a)+1]; …
Saulius
  • 21
  • 1
  • 9
2
votes
2 answers

Is strcpy where src == dest strictly defined?

It's not too hard to demonstrate that strcpy on overlapped source and destination addresses fails on some platforms, either producing incorrect results or trapping (the latter with some negative random offsets on Linux/amd64). I'd instrumented a…
Peeter Joot
  • 7,848
  • 7
  • 48
  • 82
2
votes
4 answers

Using malloc with a structure and strcpy

I'm attempting to make an array of the structure I made called StatusItem, which looks like this: typedef struct { char* name; char* index; int optional; } StatusItem; Also, as I want this array to be of any size, I am using malloc. …
Nealon
  • 2,213
  • 6
  • 26
  • 40
2
votes
1 answer

undefined reference to `strcpy_s' can't compile

i follow the book and can't compile this example. Any suggestions? 1 #define __STDC_WANT_LIB_EXT1__ 1 2 #include 3 #include 4 5 6 int main(void) 7 { 8 char source[] = "Here we go..."; 9 char…
medis
  • 31
  • 1
  • 2
2
votes
5 answers

Will delete[] after strcpy cause memory leak?

char* myChar=new char[20]; char* myChar2="123"; strcpy(myChar, myChar2); ... delete[] myChar; My question is if strcpy puts a '\0' at the end of "123", then will delete[] myChar only delete the first 3 chars and fail to delete the rest of…
Michael
  • 673
  • 2
  • 5
  • 23
2
votes
1 answer

Segmentation fault with strcpy() with array of pointers to structures

Constants: #define MAX_OPCODE_NAME_LEN 4 I have an array of structs: OPCODE *mot[NUM_OPCODES]; Struct def: typedef struct opcode { char name[MAX_OPCODE_NAME_LEN + 1]; char format; int type; } OPCODE; In my code: strcpy(mot[0]->name,…
ICantNameMe
  • 93
  • 1
  • 1
  • 7
2
votes
3 answers

How to copy the string into the array of strings with strcpy?

I'm trying to copy a string into an array of strings, but it doesn't work. I know it is a problem due to memory allocation but I don't see how I could make it work as STRING_LENGTH is a constant. #define NUMBER_OF_STRINGS 3 #define STRING_LENGTH…
edi9999
  • 19,701
  • 13
  • 88
  • 127
2
votes
1 answer

SEGMENTATION FAULT in strncpy - load from dictionary

I have this function "load" where I read words from a dictionary and put them in an hashtable of linked lists. When I try to read a line and save it in my new_node->text the compiler returns SEGMENTATION FAULT and I don't know why. The error…
2
votes
2 answers

C strcpy = Unhandled exception: Access violation writing location 0x00000000

I have a problem with strcpy function. Using C. Main point of this simple code (below) is copying a string from a array to the array of pointers. char string[20] = "ABCDEFGH\0"; char * array_of_pointers[20]; // now I want to copy string to the…
krzakov
  • 3,871
  • 11
  • 37
  • 52
2
votes
2 answers

Copy Array of strings in C error

I want to save about 20 country names into one string and then copy them to another but it always do me some mistake, can anybody help me with it? This is my code: char array1[30][30], array2[30][30]; This is how i put them into first…
Ricsie
  • 319
  • 5
  • 14
2
votes
4 answers

how can I use strcpy safely

I want to assign a char* to char* , if I use strcpy I get several run time memory problem so I fix it by simple assignment using = operator. Can any one explain what should prepare before using strcpy to avoid memory issues. EDIT: int…
Oumaya
  • 655
  • 4
  • 18
  • 43
2
votes
8 answers

Access violation when using strcpy?

I've tried reinventing the strcpy C function, but when I try to run it I get this error: Unhandled exception at 0x00411506 in brainf%ck.exe: 0xC0000005: Access violation writing location 0x00415760. The error occurs in the *dest = *src; line.…
Javier
  • 4,552
  • 7
  • 36
  • 46