I came across this question, while reading about std::array and std::vector.
1 Answer
A C-Style array is just a "naked" array - that is, an array that's not wrapped in a class, like this:
char[] array = {'a', 'b', 'c', '\0'};
Or a pointer if you use it as an array:
Thing* t = new Thing[size];
t[someindex].dosomething();
And a "C++ style array" (the unofficial but popular term) is just what you mention - a wrapper class like std::vector (or std::array). That's just a wrapper class (that's really a C-style array underneath) that provides handy capabilities like bounds checking and size information.
7 Comments
Loki Astari
Still more common to think of std::vector<> as the C++ array than std::array<>
Seth Carnegie
@Keith not by a strict definition but you can use it as one with nearly identical effects.
Keith Thompson
@Seth: Perhaps, but the common misconception that arrays are really just pointers causes a lot of problems. Perhaps it's clearer to say that array objects are not pointer objects. (Array expressions do usually -- but not always -- decay to pointer expressions.)
Seth Carnegie
@Keith yes you're right. If you think that difference is important enough to this questioner then I will add a note in my answer to that effect. My point was to show him how items are grouped in a structure that is commonly called an array, and since whenever you try to do almost anything with an array it becomes a pointer, I thought is put that in there too. I would add though that a vector isn't an array either and there's not really such a thing as C++ style arrays in the strictest sense, but I thought everyone would know I'm not giving a pedantic definition of the term.
Keith Thompson
@Seth: For example, you can have a variable and a parameter with seemingly identical declarations (
int arr[42];), but applying sizeof to the variable gives you the array size, while applying sizeof to the parameter gives you the size of a pointer. The language goes to a lot of effort to make it seem like arrays and pointers are interchangeable, by interpreting array parameter declarations as pointer declarations and by converting most array expressions to pointer type. Which means you can get away with conflating them most of the time -- and then some corner case bites you. |