strcpy

C strcpy() - evil?

Some people seem to think that C's strcpy() function is bad or evil. While I admit that it's usually better to use strncpy() in order to avoid buffer overflows, the following (an implementation of the strdup() function for those not lucky enough to have it) safely uses strcpy() and should never overflow: char *strdup(const char *s1) { ...

strcpy... want to replace with strcpy_mine which will strncpy and null terminate

the clue is in the title but basically I've inherited some code which has 800+ instances of strcpy. I want to write a new function and then to replace strcpy with strcpy_mine. So I'm trying to work out what parameter list strcpy_mine will have. I tried: void strcpy_mine( char* pTarget, const char* const pCopyMe ) { const unsigned i...

What is the difference between pointer and array in the following context?

#include <cstring> int main() { char *pName = new char[10]; char dummy[] = "dummy"; strcpy(pName + 0,dummy);//how this is different from -->this works strcpy(pName[0],dummy);//this one...--> error C2664: 'strcpy' : //cannot convert parameter 1 //from 'char' to 'c...

Why on earth would anyone use strncpy instead of strcpy?

Edit: I've added the source for the example. I came across this example: char source[MAX] = "123456789"; char source1[MAX] = "123456789"; char destination[MAX] = "abcdefg"; char destination1[MAX] = "abcdefg"; char *return_string; int index = 5; /* This is how strcpy works */ printf("destination is originally = '%s'\n", destination); r...

What's wrong with this C code?

I've tried reinventing the strcpy C function, but when I try to run it I get this error: Unhandled exception at 0x00411506 in brainf%ck.exe: 0xC0000005: Access violation writing location 0x00415760. The error occurs in the *dest = *src; line. Here's the code: char* strcpy(char* dest, const char* src) { char* dest2 = dest; whi...

Why must a pointer to a char array need strcpy to assign characters to its array and double quotes assignment will not work?

The first example does not work when you go to delete the pointer. The program either hangs when I add the null terminator or without it I get: Debug Assertion Failed Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) from Visual Studio 2008 //Won't work when deleting pointer: char *at = new char [3]; at = "tw"; // <-- not sure what'...

strcpy when dest buffer is smaller than src buffer

I am trying to understand the difference/disadvantages of strcpy and strncpy. Can somebody please help: void main() { char src[] = "this is a long string"; char dest[5]; strcpy(dest,src) ; printf("%s \n", dest); printf("%s \n", src); } The output is: this is a long string a long string QUESTION: I dont understand, how the sou...

Not copying char arrays, function swap doesnt compile correctly and stringPtr is not modified

//In header file: class definition: class myString { public: myString(void); myString(const char *str); myString(const myString &); //copy constructor ~myString(void); //destructor void swap(myString &from); private: char *stringPtr; int stringLen; }; //in cpp file, defining them member fun...

Why do I get a strcpy runtime error in my code?

I've been trying to make my code work on Windows (moved from the Mac) and for some reason I get a runtime error related to my strcpy call. Please help!! Cust.h /* * Cust.h * Project 3 * * Created by Anthony Glyadchenko on 11/17/09. * Copyright 2009 __MyCompanyName__. All rights reserved. * */ #include <iostream> #include <s...

Program crash with pointers trying to make strcpy-like .

Hi all , This is my second problem today, pointers are giving me nightmares . I'm trying to make a program that do the same thing that strcpy() function do.. Once i try it..it crashes and i'm 100% sure that's a pointers issue in my code. I think because there is some sort of an unintiallized pointer(*copied) ..But i've assigned NULL to i...

Copy results of strtok to 2 strings in C

Ok, so I have the code char *token; char *delimiter = " "; token = strtok(command, delimiter); strcpy(command, token); token = strtok(NULL, delimiter); strcpy(arguments, token); and it gives me EXC_BAD_ACCESS when i run it, and yes, command and arguments are already defined. ...

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

Hi all, 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 ( cond1 ){ I need to capture the "while" and the "cond1" into separate strin...

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 answer. Can anyone help explain this to me? It is an array of sorts so I would think you would...

strcpy v/s memcpy

What is the difference between memcpy() and strcpy()? I tried to find it with the help of a program but both are giving the same output. int main() { char s[5]={'s','a','\0','c','h'}; char p[5]; char t[5]; strcpy(p,s); memcpy(t,s,5); printf("sachin p is [%s], t is [%s]",p,t); return 0; } Output sachin p i...

strcpy() return value

A lot of the functions from the standard C library, especially the ones for string manipulation, and most notably strcpy(), share the following prototype: char *the_function (char *destination, ...) The return value of these functions is in fact the same as the provided destination. Why would you waste the return value for something r...

Why no sanity checks in legacy strcpy()

Following is the most popular implementation of strcpy in traditional systems. Why dest and src are not checked for NULL in the start? I heard once that in old days the memory was limited so short code was always preferred. Will you implement strcpy and other similar functions with NULL pointer checks at the start now days? Why not? cha...

strcpy in a struct gives SIGABRT

Hi, I'm currently working on an FTP client written in C, and it's working pretty good. I was successful in writing a function that connects to an FTP server and logs in with a username or password, but I'm having a problem with returning errors. I have setup a struct FTPError {}; with 3 fields: int An error code int An FTP error domain...

strcpy() and arrays of strings

I need to store the input from a user into an array of strings. #include <stdlib.h> #include <stdio.h> #include <string.h> char *history[10] = {0}; int main (void) { char input[256]; input = "input"; strcpy(history[0], input); return (EXIT_SUCCESS); } Running it on the terminal I get a Segmentation Fault and in ...