• 15
• 15
• 11
• 9
• 10

# char* to char [ ] with strcpy_s

This topic is 4171 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, while compiling with VS2005 I got the typical warning about "'strcpy': name was marked as #pragma deprecated" so I tried to solve this, which I had already done with a fopen in the same project. However, I didn't figure out I had to sizeof(): strcpy_s(CharArray, sizeof( ), Name); What should I sizeof() ? Thanks.

##### Share on other sites
Quote:
 Yahoo! sayserrno_t strcpy_s( char *strDestination, size_t sizeInBytes, const char *strSource );strDestinationLocation of destination string buffersizeInBytes, sizeInWordsSize of the destination string buffer.strSourceNull-terminated source string buffer.Return ValueZero if successful; an error otherwise.

##### Share on other sites
OK, but what I'd like to know is how to sizeof() an array.

##### Share on other sites
Well then....
Quote:
 MSDN saysint array[10];printf("%d", sizeof(array)); // should produce 40char* pArray = array;printf("%d", sizeof(pArray)); // should produce 4//also ....printf("%d", sizeof(char) * 11); /*if you want the destination array to be 10 characters long then you take the sizeof() a char and multiply it by n + 1 (in this case 10 + 1). the reason being that the last character in a array or pointer string should be '\0' (aka. NULL)*/

##### Share on other sites
Quote:
 Original post by longjohnsliverOK, but what I'd like to know is how to sizeof() an array.

For the sizeof() value, you should use the maximum number of bytes that your destination buffer can hold (this is basically equivalent of doing a strncpy) - I don't know why VS 2005 is ultra-picky about it's "ansi-compliance" (or at least trying to be...)...

If possible, however, you should try and use std::string if possible... (not for all cases, but if you just need basic string support)

- Or - the alternative is to disable these warnings/errors in VS 2005 (if possible).

##### Share on other sites
Quote:
 Original post by longjohnsliverOK, but what I'd like to know is how to sizeof() an array.

Don't.

Doing so is not reliable, because most C and C++ programmers haven't really grasped the highly transient nature of arrays in those languages. Specifically, arrays are not first-order addressable objects, so when you pass an array across a call point, you end up passing only the address of the first element and losing all associated type information (specifically, array length). This means that applying sizeof to an array within the scope where it is created will yield the array size in bytes, but applying it to the pointer that receives an array argument in a function will only yield the size of the pointer.

The proper approach is to explicitly store the length of the array yourself, then multiply that by the size of each array element. Or, if you are using C++, use std::string and std::vector, because they manage their own lengths, among several other benefits.
void some_function(char *data, size_t length){  ...  strcpy_s(data, length, destination);  // note that strcpy_s takes the length in CHARACTERS. if you were performing a   // memcpy, then you'd do memcpy(data, dest, length * sizeof(char));  ...}...char array[10];...some_function(array, 10);

Much more fundamentally, if you are using C++ you shouldn't be using strcpy OR strcpy_s. std::string provides all the copy operations you need in a much more intuitive syntax and with far less overflow vulnerability.

##### Share on other sites
OK, thanks!
For now I used sizeof(char)*NumberOfElementsInArray, but I'm going to try to change this into std::string.