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

What is causing this strcpy segfault?

Here is my code and it faults here strcpy(pSrcString,"muppet"); Well infact it does whenever I use strcpy. #include #include #include int main(void) { char *pSrcString = NULL; char *pDstString = NULL; /* muppet ==…
DannyK
  • 267
  • 2
  • 12
2
votes
5 answers

strcpy and printf a multidimensional char array C

Say I have an array char messages[10][2][50]; What is the correct syntax for strcpy, in order to get the data into one of the strings (inner most char array of size 50) and then the corresponding convention to supply it to printf via %s? For that…
user1611172
  • 45
  • 1
  • 6
2
votes
4 answers

Strings and string functions in C

I wrote the following: #include #include char* getString(); char* getString(){ char str[10]; gets(str); return str; } int main() { char* s; s=getString(); strcpy(s,"Hi"); puts(s); return…
Numerator
  • 1,389
  • 3
  • 21
  • 40
2
votes
4 answers

C programming: Replace an inner string using strcpy?

I've copied an HTML file into an array using the following code: fseek(board, 0, SEEK_END); long int size = ftell(board); rewind(board); char *sourcecode = calloc(size+1, sizeof(char)); fread(sourcecode, 1, size, board); Now my goal is to replace a…
Nth.gol
  • 746
  • 9
  • 20
2
votes
1 answer

_tcsncpy_s() write over old content?

Does using _tcsncpy_s() on a string multiple times write over the old contents? Or does it create new contents and then point to the new contents? As a simple example, if i have: LPTSTR myString = new TCHAR[MAX_PATH]; LPTSTR copiedString1 =…
JHowzer
  • 3,684
  • 4
  • 30
  • 36
2
votes
2 answers

memcpy_s and strcpy_s error but manual for loop works

In my example, strcpy_s and malloc_s throw an error while manual copying seems to work. here's the code. This works: hookaddrinfoluacode=(char *)malloc(16384); // This works for(i=0;i
Konrads
  • 2,206
  • 2
  • 30
  • 45
2
votes
3 answers

Segmentation Fault in strcpy()

I have a basic structure like this typedef struct struck { char* id; char* mat; int value; char* place; } *Truck; And afunction like this which creates a new "instance" of that struct: Truck CTruck(char* id, char* mat, int value,…
MrFabio
  • 586
  • 2
  • 15
  • 31
1
vote
5 answers

strncpy char string issue when adding length

I'm having a problem with comparing 2 char strings that are both the same: char string[50]; strncpy(string, "StringToCompare", 49); if( !strcmp("StringToCompare", string) ) //do stuff else //the code runs into here even tho both strings are the…
codrgi
  • 211
  • 1
  • 4
  • 10
1
vote
2 answers

How to store lines from a file into a dynamic array and print?

I need, in ANSI C, to open a file, to read all of its lines into a dynamically allocated array of strings, and to print the first four lines. The file may be any size up to 2^31-1 bytes, while each line is at most 16 characters. I have the…
cm007
  • 1,352
  • 4
  • 20
  • 40
1
vote
1 answer

what is the reason for strcpy error

When I try to execute this program I am getting segmentation fault. What could be the reason? #include #include #include #define UWT unsigned int #define DIR_LEN 1024 typedef struct…
Thangaraj
  • 3,038
  • 7
  • 40
  • 43
1
vote
4 answers

C++ Borland char * and strcpy

char *dum[32]; strcpy(&dum,InstList->Lines->Text.c_str()); InstList is a TMemo of C++ Builder Why am I getting this error? [C++ Error] emulator.cpp(59): E2034 Cannot convert 'char * *' to 'char *' Full parser context …
Hakon89
  • 1,011
  • 2
  • 9
  • 17
1
vote
2 answers

a heap-buffer-overflow with my c code when i use recursion to solve leetcode task 22

When i run my c code my solution is #include #include #include #define MAX_LEN 1000 int isValid(char* curStr) { char* ptr = curStr; int leftCount = 0; while (*ptr != '\0') { if (*ptr == '(') { …
khamun7
  • 13
  • 3
1
vote
0 answers

Why isn't strncpy working to copy a string to a variable of a struct?

I have the following struct in my code: typedef struct { uint64_t pulse; fp_t volume; fp_t factor; // Factor en pulsos / unidad de volumen char *unit_volume; char *unit_time; } totalizer_t; and it is initialized as static with 3…
1
vote
3 answers

How avoid I a comparation with NULL in strcmp?

I'm tring to make this program work, but after giving it a lot of turns, I decided is time to ask. No matter what I do the result is always a segmentation fault and I pretty much sure that is for the strcmp. Can you help me please? #include…
1
vote
4 answers

strncpy vs direct assignment seg fault

The following code below runs without a seg fault #include #include #include struct node { char *data; struct node *next; }; int main(void) { struct node *head = malloc(sizeof(struct node)); …