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
3
votes
3 answers

strcpy pass initialized null pointer c

I'm trying out the following code: int main() { char *yo = "yo"; char *whaddup = NULL; strcpy(whaddup,yo); } and I get a segfault. Complete C noob here - other places say I should initialize whaddup as an array. Why can't I pass in a…
user2635088
  • 1,598
  • 1
  • 24
  • 43
3
votes
1 answer

C++ - strcpy_s() always fails even if copying just one byte to big buffer

I feel like I've always used strcpy to copy strings without having any trouble, but I haven't done it for a long time and now I can't get it to work no matter what I do. I'm sure there's something dumb I'm missing, but I've been frustrated with this…
3
votes
5 answers

About pointers and strcpy() in C

I am practicing allocation memory using malloc() with pointers, but 1 observation about pointers is that, why can strcpy() accept str variable without *: char *str; str = (char *) malloc(15); strcpy(str, "Hello"); printf("String = %s, Address =…
Gelo
  • 65
  • 7
3
votes
7 answers

C++ Why isn't call by reference needed for strcpy()

I have a homework assignment with a number of questions. One is asking why the strcpy() function doesn't need the call by reference operator for CStrings. I've looked through the book numerous times and I can't, for the life of me, find the…
Ribbs
  • 31
  • 2
3
votes
1 answer

warning: incompatible implicit declaration of built-in function 'strlen' and 'strcpy'

I just finnished my hangman game and as a last step I am doing some code cleanup and optimization, but I can't seem to understand why I receive the following two warnings: warning: incompatible implicit declaration of built-in function…
rampy
  • 33
  • 1
  • 1
  • 4
3
votes
3 answers

Strcpy() corrupts the copied string in Solaris but not Linux

I'm writing a C code for a class. This class requires that our code compile and run on the school server, which is a sparc solaris machine. I'm running Linux x64. I have this line to parse (THIS IS NOT ACTUAL CODE BUT IS INPUT TO MY PROGRAM): while…
Dang Khoa
  • 5,693
  • 8
  • 51
  • 80
3
votes
4 answers

Using strcpy() with dynamic memory

My code runs properly and has no memory leaks. However, I am getting valgrind errors: ==6304== 14 errors in context 4 of 4: ==6304== Invalid write of size 1 ==6304== at 0x4A0808F: __GI_strcpy (mc_replace_strmem.c:443) ==6304== by 0x401453:…
3
votes
1 answer

format ’%s’ expects argument of type ’char *’

For exercising my programming skills in C I'm trying to write the strncpy function by myself. Doing that I kept hitting errors, solving most of them eventually I'm stuck with no further inspiration to go on. The error I receive is: ex2-1.c:29:3:…
mjrc
  • 43
  • 1
  • 1
  • 5
3
votes
1 answer

Implementing a strcpy function in C

My task is like this: I need to implement the strcpy function under the following constraints: The function can have no more than seven statements. It should be as fast as possible. It should use the minimum amount of memory it can. In the function…
Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
3
votes
1 answer

strcpy behaving differently on ios7

IOS7 seems to come with a new implementation (optimisation maybe) of strings strcpy. Before I was able to copy strings from any position of the array but now if I start copying from any position where (i % 4 != 0) it will crash. To show this I ran…
Mariano Latorre
  • 719
  • 1
  • 10
  • 21
3
votes
3 answers

assignment of string using strcpy and equal operator

What is difference between the following code? 1. char *p; strcpy(p,"String"); 2. char *p; p = "String"; The pointer points to the same string, but is there any difference?
Devraj Jaiman
  • 83
  • 1
  • 2
  • 8
3
votes
4 answers

Will memcpy copy a string correctly?

I am working on implementing some low-level file writing, where the file format is specific down to each bit. I need to copy a string from an NSString into null-terminated string with length 16 (which is not assignable, according to Xcode). I am a…
Phil
  • 35,852
  • 23
  • 123
  • 164
3
votes
1 answer

Implementation of strcpy and strcat that get a reference of a pointer bug

Possible Duplicate: Any better suggestions for this c functions copyString,concatString This is a question form a job interview , I need to implement it with a specific signature this is the code I need to work: int main(int argc, char…
user1120007
  • 268
  • 3
  • 13
3
votes
3 answers

Is there anyway to check the success of the str functions?

I was looking through the manuals on strcpy() and strcat(). Seems there's no way to evaluate the "success" of the function call. (ie return value will never be NULL), is that correct? It's just assumed that if you follow the rules for the input of…
Mike
  • 47,263
  • 29
  • 113
  • 177
3
votes
3 answers

Conditional jump valgrind with char* and << operator

I'm coding my version of the String class, but Valgrind whines about my implementation of the << operator for my string. The error is at the wrong line, if I print char by char it works great. Where am I wrong? Valgrind error: ==2769== Conditional…
bagage
  • 1,094
  • 1
  • 21
  • 44