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

I don't understand this while condtition in C

The task is to reproduce the behavior of the function strcpy in C. I found this code: char *ft_strcpy(char *s1, char *s2) { int i; i = 0; while (s2[i]) { s1[i] = s2[i]; i++; } s1[i] = s2[i]; return…
mr_bean
  • 5
  • 3
0
votes
1 answer

How can the strcpy function copy a large string into a smaller string?

In the following code it seems that I can copy an entire large string into a small string. So my question, how does that work? I only allocated 2 characters to str1, but it was able to store a longer string. Does this mean that strcpy is modifying…
Lakmal Caldera
  • 1,001
  • 2
  • 12
  • 25
0
votes
1 answer

Clarification on strcpy

I am currently learning C via edube.org and came across something that didn't quite agree with my observations when I run the code in Code blocks. For example: edube.org says that The invocation: strcpy(string, "Alice has a cat"); fills the array…
blessedk
  • 71
  • 7
0
votes
1 answer

In code blocks, Strcpy does not work when I declare a string using char * string but works when I declare it as char string[ ]

#include #include int main() { char t1string[] = "test"; printf("First String value is %s\n", t1string); strcpy(t1string, "lgs"); printf("Modified First String value is %s\n", t1string); char * t2string = "test"; printf("Second…
blessedk
  • 71
  • 7
0
votes
1 answer

Failed to use strcpy() in stack

this is a code about push strings and pop strings from a stack, the string name must be fixed (not input by user). But the code is unable to run (it cannot display anything). And the point is because strcpy(temp->id,val);, if i get rid of…
Scarlet
  • 1
  • 1
0
votes
1 answer

Unknow error using strtok() inside strcpy()

I'm trying remove line break from input using strtok() and pass it to struct property using strcpy() but when i execute this the Visual Studio Code is returning this message: My code: #include #include #include…
0
votes
1 answer

Strncpy issue with Double Pointer Char Array

I can't seem to figure out what is wrong with my code below: I have a double pointer char array, declared and initialized with the following: unsigned char **buffer = (unsigned char**) malloc (num * sizeof(char*)); for ( i = 0; i < num; i++ ) { …
Tim Z
  • 3
  • 4
0
votes
3 answers

Copying strings to an array

In my code's output I find that the strings I am printing are all (null) even though I input count strings of characters < 31. char name[31]; char *names[32]; int count = 5; for (int i =0; i
0
votes
0 answers

segmentation fault on copying string to dynamically allocated structure attribute using strcpy

int main(){ ... //Here, other parts of the code are correctly written. switch(n){ case 1:{ printf("Enter the path to image to be inserted: "); char img[30]; …
0
votes
1 answer

How can I fix " no suitable constructor exists to convert from const char"?

I am new to c++ and I have an error. When I created a new object the compiler giving to me " no suitable constructor exists to convert from const char[16] to stringex "and "no suitable constructor exists to convert from const char[14] to stringex…
0
votes
0 answers

can't copy string into linked list by using strcpy()

have problem copying the string in array into the linked list typedef struct{ char census_year[MAX_VAL_LEN + 1]; } data_info_t; /* struct of node */ typedef struct node node_t; struct node{ data_info_t *info; node_t *next; }; char…
0
votes
2 answers

Reading from a file, C

I am currently working on a a program whose goal is to take words from a file, and store them into an array, while outputting if they are at the beginning of a row, or a word on the line. The file looks like this: at the end of each line there is a…
0
votes
3 answers

Segmentation fault while using strcpy()?

I have a global structure: struct thread_data{ char *incall[10]; int syscall arg_no; int client_socket; }; and in main() char buffer[256]; char *incall[10]; struct thread_data arg_to_thread; strcpy(incall[0],buffer); /*works…
Lipika Deka
  • 3,774
  • 6
  • 43
  • 56
0
votes
1 answer

Using strings or pointers to strings in struct

I think I am missing something critical and I can't find an answer. If I want to use string in struct, is it better to use char arrays or pointers to them? Following code works as intended, but raises warnings "Warning C4047 'function': 'const…
Swajn
  • 1
0
votes
3 answers

C catching strcat buffer overflow

This subprogram takes three user inputs: a text string, a path to a file, and a 1 digit flag. It loads the file into a buffer, then appends both the flag and the file buffer, in that order, to a char array that serves as a payload. It returns the…
garter_snake
  • 11
  • 1
  • 2