char* strcpy(const char* src, char* dst) { char* tmp = dst; while ((*dst++ = *src++) != '\0'); return tmp; }
The strcpy function is declared in the <string.h> header of the C standard library. The function is used to copy a null-terminated string (a null-terminated array of type char). The function accepts two arguments: a pointer to the memory allocation where the copy will be placed; and a pointer to the first character of the null-terminated string to be copied. The memory allocation where the copy will be made must be large enough to accommodate the string, including the null-terminator. Example usage: void f (char* s) { int len; char* c; len = strlen (s) + 1; /* determine length of string (including null-terminator) */ c = malloc (len); /* allocate memory for copy */ strcpy (c, s); /* ... */ free (c); /* release allocation */ }
In short, you don't. strncpy is deemed unsafe as it has potential to cause buffer overruns. To copy strings safely in C++, use std::string instead. For examples and syntax, see related links, below.
char *strmerge (char *s3, const char *s1, const char *s2) { strcpy (s3, s1); strcat (s3, s2); return s3; }
By reference. The name of the string is converted to a pointer (in C/C++) and given to the function as the address of the first element. (In Java, all objects are passed by reference, and there are no pointers.)
strlen, strcpy, strcmp and many others. Consult your help system.
The strcpy function is declared in the <string.h> header of the C standard library. The function is used to copy a null-terminated string (a null-terminated array of type char). The function accepts two arguments: a pointer to the memory allocation where the copy will be placed; and a pointer to the first character of the null-terminated string to be copied. The memory allocation where the copy will be made must be large enough to accommodate the string, including the null-terminator. Example usage: void f (char* s) { int len; char* c; len = strlen (s) + 1; /* determine length of string (including null-terminator) */ c = malloc (len); /* allocate memory for copy */ strcpy (c, s); /* ... */ free (c); /* release allocation */ }
strcpy
memcpy is general purpose copy. and strcpy is specific for string copying. strcpy will copy the source string to destination string and terminate it with '\0' character but memcpy takes extra argument which specifies the number of bytes to copy.memcpy will not handle copying of overlapping memory. use memove instead.
In short, you don't. strncpy is deemed unsafe as it has potential to cause buffer overruns. To copy strings safely in C++, use std::string instead. For examples and syntax, see related links, below.
char *strmerge (char *s3, const char *s1, const char *s2) { strcpy (s3, s1); strcat (s3, s2); return s3; }
strtok sequentially truncate string if delimiter is found. If string is not NULL, the function scans string for the first occurrence of any character included in delimiters. If it is found, the function overwrites the delimiter in string by a null-character and returns a pointer to the token, i.e. the part of the scanned string previous to the delimiter. After a first call to strtok, the function may be called with NULL as string parameter, and it will follow by where the last call to strtok found a delimiter. delimiters may vary from a call to another. Parameters. string Null-terminated string to scan. separator Null-terminated string containing the separators. Return Value. A pointer to the last token found in string. NULL is returned when there are no more tokens to be found. Portability. Defined in ANSI-C.
By reference. The name of the string is converted to a pointer (in C/C++) and given to the function as the address of the first element. (In Java, all objects are passed by reference, and there are no pointers.)
// Inventory Pointer // Demonstrates returning a pointer #include <iostream> #include <string> #include <vector> using namespace std; //returns a pointer to a string element string * ptrToElement(vector<string>* const pVec, int i); int main() { vector<string> inventory; inventory.push_back( "sword"); inventory.push_back( "armor"); inventory.push_back( "shield"); //displays string object that the returned pointer points to cout << "Sending the objected pointed to by returned pointer:\n"; cout << *(ptrToElement(&inventory, 0)) << "\n\n"; //assigns one pointer to another - inexpensive assignment cout << "Assigning the returned pointer to another pointer.\n"; string* pStr = ptrToElement(&inventory, 1); cout << "Sending the object pointed to by new pointer to cout:\n"; cout << *pStr << "\n\n"; //copies a string object - expensive assignment cout << "Assigning object pointed by pointer to a string object.\n"; string str = *(ptrToElement(&inventory, 2)); cout << "Sending the new string object to cout:\n"; cout << str << "\n\n"; //altering the string object through a returned pointer cout << "Altering an object through a returned pointer.\n"; *pStr = "Healing Potion"; cout << "Sending the altered object to cout:\n"; cout << inventory[1] << endl; return 0; } string * ptrToElement(vector<string>* const pVec, int i) { //returns address of the string in position i of vector that pVec points to return &((*pVec)[i]); }
strlen, strcpy, strcmp and many others. Consult your help system.
int mystrlen (const char *s) { const char *t; if (!s) return 0; for (t=s-1;*++t;); return t-s; }
use strcat, strncpy, stpcpy, sprintf, strlen+memcpy, etc
We use a pointer to reference a string because a string is an array of characters where every element is a char (or a wchar_t if using UNICODE strings). Passing arrays by value would require the entire array to be copied, but passing a pointer variable to an array only copies the pointer, which is effectively the same as passing the array by reference. #include <iostream> int main() { char * psz = "hello"; // pointer to a null-terminated string. std::cout << psz; // pass the pointer (by value) to the insertion operator. return( 0 ); }