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
1
vote
3 answers

strcpy() causes segmentation fault?

Possible Duplicate: Getting Segmentation Fault Why does this code cause a segmentation fault? char *text = "foo"; strcpy(text, ""); As far as I understand it, the first line allocates some memory (to hold the string "foo") and text points to…
Thomas
  • 17,016
  • 4
  • 46
  • 70
1
vote
2 answers

'char' array issue, custom print function and strcpy_s problem

I just started C++ and now I'm making a simple program. But don't know how to fix this problem. I'm not a native english speaker so some sentences may not be understandable. int main() { char test[5][4] = { "abc", "def", "ghi", "jkl", "mno" }; …
JunGi_Lee
  • 13
  • 3
1
vote
1 answer

C strcpy segmentation fault when copying from another function

This is a snippet of the code from a project made in the programming class at my college, and my problem is that I get a segmentation fault error when I get to the strcpy part and I have no idea why. I don't know if it's relevant or not, but I am…
Hiroi
  • 21
  • 1
1
vote
1 answer

Access violation with strcpy in c

#include #include #include #define SIZE_OF_RECORD 100 void main() { char lineBuffer[SIZE_OF_RECORD]; memset(lineBuffer, '\0', (SIZE_OF_RECORD - 1) * sizeof(char)); strcpy(lineBuffer, "testing");…
1
vote
3 answers

What's causing a segmentation fault in my code and what I should do to fix it?

I've been getting segmentation fault and I don't know why. I have a feeling that it's caused by the code under if(isPalindrome(str_array[i]) == 0) but I don't know which one and what to do about it. P.S. I'm still a student so I would appreciate it…
1
vote
3 answers

strcpy and strcat cause problems sometimes

hello I have a code like the one below char *str ; strcpy(str, "\t<"); strcat(str, time); strcat(str, ">["); strcat(str, user); strcat(str, "]"); strcat(str, "("); strcat(str, baseName); …
kanoz
  • 15
  • 1
  • 3
1
vote
1 answer

strcpy is not working well with pointer array

#define _CRT_SECURE_NO_WARNINGS #include #include int main(int argc, char *argv[]) { char *ptr; char temp[20]; if (strlen(argv[1]) < strlen(argv[2])) { strcpy(temp,argv[1]); …
urasiapan
  • 11
  • 2
1
vote
2 answers

Why strcpy(buffer +i, buffer + i +1) doesn't work in the same way as using a temporay array?

#include #include int main() { char buffer[200] = "This is a string"; char buffer1[200]; strcpy(buffer1, buffer); char vowels[] = "aeiouAEIOU"; int i; for (i = 0; i < strlen(buffer); i++) { if…
sandrino
  • 65
  • 1
  • 7
1
vote
2 answers

Add strings to an array

The problem: After the convert_tolower(words) function is completed I want to add a new word in the words array( if the words array has less than 5 words)..But I am getting either errors or unexpected results(e.g some weird characters being…
George S
  • 51
  • 7
1
vote
2 answers

why do i get Segmentation fault executing this code?

I'm trying to figure out the issue with this code: int main(int argc, char *argv[]) { char *s; scanf("%s\n", s); char *t = malloc(sizeof(*s) + 1); strcpy(t, s); t[0] = toupper(t[0]); printf("s: %s\n", s); printf("t:…
1
vote
3 answers

Question about strcpy using char pointer in C

I'm doing some programming with C, and I have a minor problem using strcpy. char* file="It has something inside" int size= sizeof(file); char* file_save = malloc(sizeof(char)*size); strcpy(file_save,file); My code stopped working in the last…
lin ki
  • 39
  • 4
1
vote
2 answers

Is this snprintf method a safe way to copy strings?

I need your advice. I use this way to copy strings knowing a max size to not go over but some of these strings don't end with a null terminator. It's just a snippet. void my_strcpy(char* dest, const char* src, const size_t max_size) { …
1
vote
1 answer

Why this situation was happen? c lib problem while using Windows WSL(ubuntu 20.04)

I meet one strange problem while using gcc for c lib in strcpy and strtol function. Test on two situation and get the very different results. //#The bad code is "res=68" #include #include #include #include…
1
vote
0 answers

Strcpy does not copy from source const char * to char *

I've encountered something in a simple code test that started bothering me. I don't really understand the reason the program crashes before line with the strcpy function invocation. So basically, can someone explain to me, why does this sample of…
todovvox
  • 170
  • 10
1
vote
2 answers

How to concatenate strings formatted with sprintf in C

I have multiple sentences which are being printed in the console currently. I have to gather them into a string. A part of the code is: #include #include int main() { char buffer [100]; sprintf (buffer, "%d plus %d is…
Enchantres
  • 853
  • 2
  • 9
  • 22