char* to char [ ] with strcpy_s

Started by
5 comments, last by longjohnsliver 17 years, 6 months ago
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.
Advertisement
Quote:Yahoo! says
errno_t strcpy_s(   char *strDestination,   size_t sizeInBytes,   const char *strSource );


strDestination
Location of destination string buffer

sizeInBytes, sizeInWords
Size of the destination string buffer.

strSource
Null-terminated source string buffer.

Return Value
Zero if successful; an error otherwise.

Beginner in Game Development?  Read here. And read here.

 

OK, but what I'd like to know is how to sizeof() an array.
Well then....
Quote:MSDN says

int 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)*/

Beginner in Game Development?  Read here. And read here.

 

Quote:Original post by longjohnsliver
OK, 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).


Quote:Original post by longjohnsliver
OK, 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.
OK, thanks!
For now I used sizeof(char)*NumberOfElementsInArray, but I'm going to try to change this into std::string.

This topic is closed to new replies.

Advertisement