I was hoping that, after reading up about this, you would come to realize that
in C, arrays are pointers. Let me say that again: arrays are pointers. Arrays are just a special use case of pointers. In C, there are really just two general types of data: pointers, and non-pointers; arrays are nothing more than a common use case of pointers--so common that they created some syntax to make things look a bit cleaner--but at the end of the day, you're just playing with pointers.
When you have the following in a function:
This is essentially equivalent to:
The most important part is that the "foo" in "char foo[50];"
is a pointer. You think of it as an array, and there is array syntax in use here, but
it is just a pointer. Which was the point of my final question (the one that's a trick question): that there is no difference between those 4 snippets, that those 4 snippets do the same thing (yes, the 6th byte is uninitialized random data, but that wasn't the point; the point that I was driving at is that arrays are pointers and array access brackets and pointer dereference operators do the same thing).
This is why when you want to create a pointer that points to an integer, you use
Code:
int bar;
int *foo = &bar;
But with arrays, you use
Code:
char bar[50];
char *foo = bar;
Note the lack of the address-of operator in the second case. Why? Because bar is already a pointer--it already is a memory address.
So, if we consider
Code:
char foobar1[MAX_PATH];
char* foobar2 = new char[MAX_PATH];
char* foobar3 = malloc(MAX_PATH);
std::array<char,_MAX_PATH> foobar4;
The difference is that the first creates a pointer and allocates MAX_PATH characters to it on the stack, which means it's automatically freed when you leave the scope of the function. The second does the exact same thing, except memory is allocated on the heap instead of on the stack (memory has to be freed manually). The third does the same as the second, except malloc() is classic C and can be used in either C or C++ whereas "new" is a new syntax that works only in C++. And the final one is an entirely different beast because it's a C++ STL class and is completely unlike any of the other three.
In my first question, the correctness isn't because of strcat_s (that's just good programming practice), but because it uses char * in both the function parameters and the return. Which it does because
arrays are pointers and while pointer syntax is always valid when dealing with arrays, the specialized array syntax is used only in special circumstances. Specifically, the the number of bytes/elements in brackets
is used for allocation. So when you declared path[_MAX_PATH], the part in brackets served to tell the compiler how many bytes in your class to allocate as memory for path. But once that memory has been allocated, and you're just returning the address to that chunk of memory, you aren't allocating any more, and the [] syntax makes no sense.