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

Copying strings between vectors of strings in C

I have an array of char pointers (string array), which contains some duplicate values. I've found an algorithm that truncates the array by removing its duplicate values. Here is a code sample : int i, j , k; int n = 10; char…
davideAlbertini
  • 93
  • 2
  • 11
2
votes
4 answers

Strncpy should only be used with fixed length arrays

According to this StackOverflow comment strncpy should never be used with a non-fixed length array. strncpy should never be used unless you're working with fixed-width, not-necessarily-terminated string fields in structures/binary files. – R.. Jan…
Puddler
  • 2,619
  • 2
  • 17
  • 26
2
votes
1 answer

Why does strcpy to a literal compile?

In Visual Studio 2015, the following does compile: strcpy("destination", "Source"); Shouldn't the compiler figure out that "destination" is a literal and cannot constitute a valid non-const char* parameter? As a side note, it does "correctly" crash…
Vincent Hubert
  • 1,386
  • 9
  • 23
2
votes
1 answer

C strcmp not working

So I have the following code and basically buffer is supposed to hold a string, "NOT FOUND" which is given by the server I'm connecting to. It is done by the recvfrom() system call in the first function block. The problem is it is still going to my…
2
votes
4 answers

Reverse string in malloc

I need define a “word” in this question to be any sequence of characters that doesn’t contain a space or null character. For example, the string “Hello World” would contain 2 words. However, it is actually possible for a word to be empty i.e., zero…
Gerald Lee
  • 21
  • 2
  • 5
2
votes
2 answers

Int array empty - C

I am attempting to write a program that will take two sets of strings N and Q. The goal of the program is to print out the number of times each string in Q occurs in N. However, I am struggling to manage strings and pointers in C and in particular I…
JG7
  • 371
  • 2
  • 10
2
votes
2 answers

cases when strcpy can be use but strcat cannot

Main question is: should I ever use strcpy (from cstring library)? I often use strcat instead of strcpy in pgms like: char arr[10]; arr[0] = '\0'; strcat(arr, "hey!"); // alternatively strcpy(arr, "hey!"); I cannot think of cases where strcpy could…
hartmut
  • 934
  • 12
  • 25
2
votes
3 answers

Strange strncpy response

I just run this code and what i get for n=1 is not what i expect to get. Can you explain why is this happening? #include #include #include #define MAXRIGA 11 int main() { char s[MAXRIGA+2]; char…
Synchronyze
  • 128
  • 1
  • 10
2
votes
2 answers

Why can't I strcpy?

For an assignment, I have to make a grade book in C consisting of several interlocking functions. I've gotten through the first few without (too many) headaches, but then there's this thing. Basically, I'm trying to take a character string and copy…
2
votes
6 answers

Getting empty when using my own strcopy function

void strcopy(char *src, char *dst) { int len = strlen(src) - 1; dst = (char*)malloc(len); while(*src){ *dst = *src; src++; dst++; } *dst = '\0'; } int main() { char *src = "hello"; char *dst = ""; strcopy(src, dst); …
2
votes
3 answers

C how strcpy works and Does it change the size of the original string?

I have this code.. #include #include int main() { char a[6]="Hello"; char b[]="this is mine"; strcpy(a,b); printf("%d\n",sizeof(a)); printf("%d\n",sizeof(b)); printf("%s\n",a); …
RajSharma
  • 1,941
  • 3
  • 21
  • 34
2
votes
1 answer

strcpy source is not displayed

#include #include #include using namespace std; int main () { char a[]="one string",b[]="twostrings"; strcpy (a,b); cout<<"A="<
user4945113
2
votes
4 answers

Use strcpy to transform a C++ string to a Char array

For some reason, I was trying to covert a C++ string to a char array. Below is what I did: string aS="hello world"; char aC[aS.size()]; strcpy(aC, aS.c_str()); cout << aC[0] << endl; string bS="veooci m eode owtwolwwwwtwwj mooawee mdeeme eeeec…
2
votes
1 answer

Issue when implementing strcpy in C

For a homework assignment I'm supposed to implement all 22 functions of the string.h library (2fun2handle). I've gotten a lot of functions down, but am running into a bit of trouble when trying to implement strcpy. After quite a few revisions, here…
Alex
  • 2,145
  • 6
  • 36
  • 72
2
votes
4 answers

Segmentation fault using strcpy

I have some troubles when using strcpy to copy an array of string inside a double pointer with allocated memory, but i can't understand why i get segmentation fault even if i have previously allocated memory. Here is the code: #include…
Luca
  • 55
  • 7