1

Ok, so I am trying to fix my C++ assignment, but when I use strcpy_s it only works for my array and not my *pointer.. here is what I am working with:

HotelRoom::HotelRoom(char Num[], int cap, double daily, char* name, int Stat)
{
strcpy_s(room_Num, Num); //copy first argument into room_Num[]
guest = new char[strlen(name) +1]; //create space for the name
strcpy_s(guest, name); //copy second argument into new space
capacity = cap;
dailyRate = daily;
occupancyStat = Stat;
}

This is the error I get when using it in this manner strcpy_s(guest, name); :

"no instance of overloaded function "strcpy_s" matches the argument list argument types are: (char*, char*)".

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
LoyalNewb
  • 43
  • 1
  • 1
  • 7
  • 1
    Probably a useless question, but why don't you use a string? – Nil Nov 22 '13 at 02:49
  • different function signature? http://msdn.microsoft.com/en-us/library/td1esda9(v=vs.90).aspx – kenny Nov 22 '13 at 02:54
  • @Nil, becasue the book assignment says to use a char* and a Char[] – LoyalNewb Nov 22 '13 at 02:59
  • @kenny, I read that site before posting here; and I don't understand what it is saying. That is why I am posting here. I am a beginner. – LoyalNewb Nov 22 '13 at 03:01
  • @SyntaxEyes look at the answer from Yu below. It has different arguments or signature of the standard strcpy() or yore. – kenny Nov 22 '13 at 14:58

3 Answers3

3

The non-standard strcpy_s takes one extra parameter than std::strcpy that is the max size you want to copy.

errno_t strcpy_s(char *s1, size_t s1max, const char *s2);

What you need is the standard C function std::strcpy.

char *strcpy(char *s1, const char *s2);
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • when I use: char *strcpy(char *s1, const char *s2); it output a bunch of symbols like "==^&%$@". But when I use: – LoyalNewb Nov 22 '13 at 03:13
  • but when I use: strcpy_s(char *s1, size_t s1max, const char *s2); it works, but at end of compiling it errors with a "break" error, or something like that. – LoyalNewb Nov 22 '13 at 03:14
  • @SyntaxEyes C-style strings need to be null-terminated, make sure of that. Or better, use `std::string` if that's an option. – Yu Hao Nov 22 '13 at 03:18
  • @YuHao, FYI `strcpy_s` is a standard C11 function: http://en.cppreference.com/w/c/string/byte/strcpy. – Annie Mar 24 '15 at 04:37
  • @Annie And this is a C++ question. – Yu Hao Mar 24 '15 at 04:47
3

Take a look at the documentation: http://msdn.microsoft.com/en-us/library/td1esda9%28v=vs.90%29.aspx

When the size cannot be determined automatically because a statically sized array is not being passed you must provide it.

#include <string.h>

int main()
{
    char src[] = "Hello World!\n";
    char staticDest[100];
    size_t dynamicSize = strlen(src) + 1;
    char* dynamicDest = new char[dynamicSize];

    //Use the overload that can determine the size automatically
    //because the array size is fixed
    //template <size_t size> errno_t strcpy_s(char(&strDestination)[size], const char *strSource);
    strcpy_s(staticDest, src);

    //Use the overload that requires an additional size parameter because
    //the memory is dynamically allocated
    //errno_t strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
    strcpy_s(dynamicDest, dynamicSize, src);

    return 0;
}
Retired Ninja
  • 4,785
  • 3
  • 25
  • 35
  • isn't that what I am doing here: guest = new char[strlen(name) +1]; doesn't that make room for the pointer to hold name? – LoyalNewb Nov 22 '13 at 03:04
  • No, since you are dynamically allocating the memory you have to tell the function how large the destination buffer is. If you have a static array like `char s[100];` the templated overload can automatically determine the size. I added an example of both in the answer. – Retired Ninja Nov 22 '13 at 03:21
  • OOHHH! I see. Ok so I used your second example with the 3 arguments, and now after compile is complete it pops up with a error that says "breakpoint" then it says abort when I click "break" button. – LoyalNewb Nov 22 '13 at 03:57
  • You should be able to see in the debugger the line where the error occurred. Without seeing all of your code it's hard to tell what the problem might be. – Retired Ninja Nov 22 '13 at 04:07
  • In both of these calls to `strcpy_s` the code should check the returned value. Otherwise, it offers only a small advantage over simply calling `strcpy`: it replaces a buffer overrun with data corruption. – Pete Becker Nov 22 '13 at 18:40
  • Yeah, it's just a demonstration of the two different versions of it. In real code checking for errors is important. – Retired Ninja Nov 22 '13 at 22:22
-1

The following should work:

strcpy_s(guest, strlen(name), name); //copy second argument into new space
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Anuj
  • 1
  • 1
  • would have to be strcpy_s(guest, strlen(name)+1, name) and works around the warning and around the intention why strcpy_s was introduced anyhow. – notan Sep 30 '21 at 10:00