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

C strcpy() copies string literals without segmentation fault

To my understanding string literals are stored in read-only memory and modifying it during runtime leads to a segmentation fault, but my below code compiles without a segmentation fault. #include #include int main() { char*…
Vencat
  • 1,272
  • 11
  • 36
0
votes
0 answers

Adding pointers to an established pointer array after using strtok

I'm trying to do the following actions in my code, which is an implementation of textsort in C. -the data[] contains pointers to sentences read in from a file. make a copy of jth content of the "data" array and store it in the line buffer char *.…
0
votes
1 answer

Whats the proper way to handle strcpy's return type

I created a program which at some point turned to be a buggy one and I can not find a good way to handle situations like in the following program: #include #include #include int get_user_name( char *const dest, char…
user10533142
0
votes
2 answers

Need to understand how char * strcpy (char *cad1, const char *cad2) works in C

Can't get how a method with this head: char * strcpy (char *cad1, const char *cad2), works in C in this sample: 'char * strcpy (char *cad1, const char *cad2){ char *aux = cad1; for( ; *cad1++ = *cad2++; ); return cad1; }'
0
votes
4 answers

Updating string in C

I have a function on a small micro that sends one character at a time out the UART port. USART_0_write('x'); This causes a character x to go out the serial port. I have created this function that works: char source[] = "Test Text."; for (uint8_t…
kjav
  • 93
  • 1
  • 7
0
votes
0 answers

String is used uninitialized

I have created this very simple program. My goal is to have the output say String: hello world James but I want the hello world to be malloced in my test_function. Can someone explain to me how I can make my_intro = "hello world" and name my_name =…
Kiwa
  • 215
  • 2
  • 10
0
votes
1 answer

finding the min string in an array of chars

I'm trying to find the MIN string out of a bunch of strings in a 2D array initialized with empty strings "". The user enters some strings which are then strcpyied and then the following method gets called.However the if statement doesn't work as…
Stelios Papamichail
  • 955
  • 2
  • 19
  • 57
0
votes
1 answer

Initializing a char *tab[] with malloc/calloc

I’m trying to initialize a char *tab[] and this causes segmentation fault, what am I doing wrong? #include #include #include #include int main() { char **cle_par = NULL; cle_par = (char**) calloc…
0
votes
2 answers

Without null char in second argument for strcpy() function is returning unexpected result. Why?

#include #include int main() { char a[5]; char b[2]="12"; strcpy(a,b); printf("%s\n",a); } There is no null char in string b that is why the output is not as expected.output : 12@ Why the output is coming like…
0
votes
2 answers

String Copy in C

I have a function which I pass a character pointer to, like: funtion_name (char* string){ ... } I want to copy the string to a temporary string variable, then copy that into a struct. This is what I have done thus far: char* namecpy =…
user10497670
0
votes
1 answer

strcpy issue with structure in C

The content of course.Code before and after strcpy(course.Name, b) is "This" and "Thisis", which seems like strcpy() is also concatenating the content of b to course.Code typedef struct { char Code[4]; char Name[2]; }Course; int main() { …
Richard Tran
  • 132
  • 1
  • 11
0
votes
1 answer

Segmentation Fault when using strcpy with a linked list member

I have a text file with the following data: 11111,First,Last,2,COR,100,SEG,200 22222,F,L,1,COR,100 33333,Name,Name,3,COR,100,SEG,200,CMP,300 *** I need to separate the data in each line (at the commas) and add each line of data into a linked list. …
user7438591
  • 29
  • 1
  • 6
0
votes
3 answers

Strcpy() function parameters

I am using the function strcpy() a lot for an assignment. Looking at the function prototype for strcpy(): char *strcpy(char *dest, const char *src) It can be seen that strcpy() takes in two pointers. However, I have seen in a lot of examples online…
ceno980
  • 2,003
  • 1
  • 19
  • 37
0
votes
2 answers

Swapping the element using strcpy not working in the array

I was trying to swap the elements in the array (which is extremely basic, but when I try to change the index of the array, it mess up the whole array) #include #include int main(int argc, char **argv) { int swap; int…
0
votes
2 answers

C - Can not use strcpy with strtok

I am working with a function that accept full name as input and print them out in order: first name, middle name, last name. The problem is my while loop seem to go infinite and i haven't figured out how to fix it. Here is my function: void…
Thuan Nguyen
  • 431
  • 6
  • 18