char = is a built-in type |
Yes. It's a so called
fundamental type.
char is technically just an integer type, but it's often used for characters so many library functions will treat it differently.
and char array[] are arrays of built-in types |
A char array is a so called
compound type because it's defined in terms of another type.
but they are not classes or vectors |
Yes.
std::vector is a
class template (a template for generating classes) so std::vector<T> (where T is some type) is technically a class.
std::string is a class.
But when just writing
string in a sentence it can often mean any kind of string. C string, std::string, std::string_view, etc.
A pointer to a char array, points to what exactly? Like the other pointers, to the address of the first index? |
Assuming you mean a char*, it can point to any element in the array. But the implicit conversion from array to pointer gives you a pointer to the first element in the array. This is the case for all arrays, not just char arrays.
Because it knows the first address, but returns the whole c-style string because of the need to support legacy C and convenience. |
There is no difference in how the pointers work. They're just treated differently by various functions.
BUT even outside the cout the way the pointer to the char array behaves holds true and there is NO OVERLOADING here!....so the below line sends the whole string and not the address:
string strHello = ptrArray; // prints out "Hello World" and doesn't give address, because legacy needs this format for whole c-style string...it is just the way it is! |
In this case it's the constructor of std::string that treats the char* that is passed to it in this way. It assumes its a C string (null-terminated character array). If it isn't (i.e. the array is not null-terminated) it will lead to undefined behaviour (UB).
1 2 3
|
char buf[3] = {'A', 'B', 'C'};
std::string str = buf; // UB
std::cout << buf; // Also UB
|
And I cannot do this:
void* intAddress = ptrArray; //doesn't give address, because legacy needs this format for whole c-style string...it is just the way it is! |
That code should work. It should compile. You won't be able to do much with the void* though. You can print it and cast it back to a char* but not much more.
But I can still do it this way:
void* intAddress = (unsigned int*)ptrArray; //NO OVERLOADING! |
You
can but I'm not sure you're allowed to. There are very strict rules about what type of pointers can point to which type of data. Maybe it's OK if you don't dereference the pointer but generally you're not allowed to read an object using a pointer or reference other than the original type of the object. There are some exceptions but I don't think this is one of them.
1) Interesting that I can cast to many types and it still works, probably because all these pointers are all 8 bytes and all can hold an address. |
Just because you
can doesn't mean you're allowed to or that it is guaranteed to work. You need to be careful and know the rules. There are many pitfalls.
2) To get address of a particular index I used "(unsigned int*)(ptrArray+i)". |
I still think you should use void*.
3) At the bottom of the code you cannot even get the address of a single char "&charK" |
You can but trying to print that pointer with std::cout<< is UB because it doesn't point to a char in a null-terminated array.
and I tried (unsigned int*)charK that Gives [Warning] cast to pointer from integer of different size [-Wint-to-pointer-cast]. So it needs cast to a wide int, never did that type yet. |
Did you forget the & operator?
Remember I said char is an integer type. You're essentially trying to interpret the integer value that is stored in charK as a memory address. 'k' is typically 107 so that cast will essentially give you an int pointer to the memory location 107 (technically I believe this might be UB because there is probably no int stored at that location).