So both the older c-style cast and the newer c++ cast require (unsigned int*). So I now get that I just need to use it because of legacy C.
Just to see if I might be missing anything else let me take one step back and review it all:
char = is a built-in type and char array[] are arrays of built-in types, but they are not classes or vectors.
string = is a class.
A pointer to a char array, points to what exactly? Like the other pointers, to the address of the first index? Because it knows the first address, but returns the whole c-style string because of the need to support legacy C and convenience. The return of the pointer almost behaves like a (*this), a pointer to the thing...the whole c-style string.
1 2
|
char myCharArray[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'};
char* ptrArray = myCharArray;
|
Now, it might be true that cout << treats char* differently and that there is some overloading, 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!
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!
But I can still do it this way:
void* intAddress = (unsigned int*)ptrArray; //NO OVERLOADING!
I did some playing around in the code below and here are some notes:
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.
2) To get address of a particular index I used "(unsigned int*)(ptrArray+i)".
3) At the bottom of the code you cannot even get the address of a single char "&charK" 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
using namespace std;
int main()
{
char myCharArray[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'};
char* ptrArray = myCharArray;
cout << "&myCharArray = " << &myCharArray << endl;
cout << "&myCharArray[0] = " << &myCharArray[0] << endl; //<<<<<<<<<<<<<<<<<<<<<< prints "Hello World"
cout << "&myCharArray[1] = " << &myCharArray[1] << endl; //<<<<<<<<<<<<<<<<<<<<<< prints "ello World"
cout << "&myCharArray[2] = " << &myCharArray[2] << endl; //<<<<<<<<<<<<<<<<<<<<<< prints "llo World"
cout << "(unsigned int*)ptrArray = " << (unsigned int*)ptrArray << endl;
cout << "(unsigned int*)ptrArray = " << (unsigned int*)(ptrArray+ (sizeof(char))) << endl;
cout << "(unsigned int*)ptrArray = " << (unsigned int*)(ptrArray+1) << endl;
cout << "(unsigned int*)ptrArray = " << (unsigned int*)(ptrArray+2) << endl;
cout << "__________________________________________\n";
//Address for every index
for (int i = 0; i < sizeof(myCharArray) - 1; ++i )
cout << "(unsigned int*)ptrArray" << "[" << i << "] =" << (unsigned int*)(ptrArray+i) << endl;
cout << "ptrArray = " << ptrArray << endl; //"Hello World"
//sizeof()
cout << sizeof(char) << endl; // = 1
cout << sizeof(myCharArray[0]) << endl; // = 1
cout << sizeof(myCharArray[1]) << endl; // = 1
cout << sizeof(myCharArray[2]) << endl; // = 1
cout << sizeof(ptrArray) << endl; // = 8
cout << sizeof(unsigned int*) << endl; // = 8
cout << sizeof(char*) << endl; // = 8
cout << "__________________________________________\n";
//Interesting, can cast into any type
cout << (int*)ptrArray << endl;
cout << (long long*)ptrArray << endl;
cout << (short int*)ptrArray << endl;
cout << (double*)ptrArray << endl;
cout << (long double*)ptrArray << endl;
cout << (float*)ptrArray << endl;
cout << (string*)ptrArray << endl;
cout << sizeof((int*)ptrArray) << endl; //8
cout << sizeof((long long*)ptrArray) << endl; //8
cout << sizeof((short int*)ptrArray) << endl; //8
cout << sizeof((double*)ptrArray) << endl; //8
cout << sizeof((long double*)ptrArray) << endl; //8
cout << sizeof((float*)ptrArray) << endl; //8
cout << sizeof((string*)ptrArray) << endl; //8
cout << "__________________________________________\n";
string strHello = ptrArray;
cout << "strHello = " << strHello << endl; //"Hello World"
//void* intAddress = ptrArray; //Still does not give address, even without a cout <<
//unsigned int* intAddress = (unsigned int*)ptrArray;
void* intAddress = (unsigned int*)ptrArray;
cout << intAddress << endl;
cout << (unsigned int*)ptrArray << endl;
char charK = 'k';
cout << charK << endl << endl;
//cout << &charK << endl << endl; //does not work
cout << (unsigned int*)charK << endl; //Gives [Warning] cast to pointer from integer of different size [-Wint-to-pointer-cast]
return 0;
}
|