I understand the concept of pointers and arrays in C. My problem is the syntax of them. What is this:
char* var;
char var*;
Your premise is wrong. The first is the pointer type and the second is well... nothing, it is not valid C++ (neither is C for that matter). Static arrays are more like this:
char var[15];
Dynamic arrays are on the other hand usually done with vectors.
The confusion between arrays and pointers is a legacy from the C programming language, where *(a+n) and a[n] mean the same thing, so we can use the a[n] even if a is a pointer, not an array. This is not very idiomatic in C++ though.
char* var; declares var to be a "pointer to char". char var*; is not a valid declaration.
An array would be declared something like: char var[10];.
std::array<char, 10> var;std::string objects are strings, but strings in the base sense are still C-style character arrays. Hopefully std::array will take over in the same way one day.This is a pointer:
char* var;
This is syntactically incorrect, and will not compile:
char var*;
Arrays and pointers in C++ are not the same thing, but the syntax might make it seem like they are. You can declare an array in C++ like this:
char ary[] = {'a','b','c'};
...and this is valid -- there will be an array created with 3 elements.
You can also create a function which takes a pointer:
void foo(char* bar)
{
// ...
}
...and pass it ary:
foo(ary);
...and it will compile and run fine. This might lead you to believe that pointers and arrays are the same thing; but they aren't. bar in foo above isn't an array -- it's a pointer that points to the first element of the array.
foo(ary);, ary decays into the needed pointer.