strcpy_s weirdness
-
I was playing around in Microsoft Visual C++, and I noticed something weird about this code:
void setname(char name[80])
{
strcpy_s(name, "joebob");char name2\[80\]; strcpy\_s(name2, "joebob");
}
It looks like the compiler complains about the first strcpy_s but not the second one. If I comment out the first one, the program compiles fine. Can anyone explain this? BTW, I know strcpy_s should probably take 3 arguments, but I'm trying to understand this weirdness. Thanks.
-
I was playing around in Microsoft Visual C++, and I noticed something weird about this code:
void setname(char name[80])
{
strcpy_s(name, "joebob");char name2\[80\]; strcpy\_s(name2, "joebob");
}
It looks like the compiler complains about the first strcpy_s but not the second one. If I comment out the first one, the program compiles fine. Can anyone explain this? BTW, I know strcpy_s should probably take 3 arguments, but I'm trying to understand this weirdness. Thanks.
Remember that in C arrays and pointers are the same thing so the argument
name
is a pointer to char. For the compiler, your code fragment is the same as:void setname(char *name)
{
strcpy_s(name, "joebob");char name2\[80\]; strcpy\_s(name2, "joebob");
}
In the case of the second call to
strcpy_s
, the compiler can figure out the size parameter through some template magic. For the first one however you have to use the explicit size form.Mircea
-
I was playing around in Microsoft Visual C++, and I noticed something weird about this code:
void setname(char name[80])
{
strcpy_s(name, "joebob");char name2\[80\]; strcpy\_s(name2, "joebob");
}
It looks like the compiler complains about the first strcpy_s but not the second one. If I comment out the first one, the program compiles fine. Can anyone explain this? BTW, I know strcpy_s should probably take 3 arguments, but I'm trying to understand this weirdness. Thanks.
-
I was playing around in Microsoft Visual C++, and I noticed something weird about this code:
void setname(char name[80])
{
strcpy_s(name, "joebob");char name2\[80\]; strcpy\_s(name2, "joebob");
}
It looks like the compiler complains about the first strcpy_s but not the second one. If I comment out the first one, the program compiles fine. Can anyone explain this? BTW, I know strcpy_s should probably take 3 arguments, but I'm trying to understand this weirdness. Thanks.