Ok, here I come with a some sort of big analysis with pointer, array, vector, iterator and address of memory...
using this input:
// hakim makan nasi
// ayam yang basi
// sambil tengok
// hafiz yang tergelak
// gelak
with this code:
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
const int maxStrings = 5;
vector<string> svec(maxStrings);
char* parr[maxStrings] = { NULL, NULL, NULL, NULL, NULL };
for (int i = 0; i != maxStrings; ++i)
{
getline(cin, svec[i]);
parr[i] = new char[svec[i].size() + 1];
strcpy(parr[i], svec[i].c_str());
}
vector<string>::iterator str_iteration = svec.begin();
for (char **curr_point = parr, **bound_point = parr + maxStrings;
curr_point < bound_point; ++curr_point)
{
cout << parr << endl; // 1
cout << curr_point << endl; // 2
cout << *str_iteration << endl; // 3
cout << *parr << endl; // 4
cout << **parr << endl; // 5
cout << *curr_point << endl; // 6
cout << **curr_point << endl; // 7
cout << &str_iteration << endl; // 8
cout << &parr << endl; // 9
cout << &curr_point << endl; // 10
}
delete [] parr;
cout << endl << endl << endl;
return 0;
}
see the attachment(Microsoft Excel) for the details that I listed. It contain each output result from the above code and another detail/description that explained and grouped...
before going to conclusion for the analysis, take a note for this, based on the details in the attachment :
reference : see in the attachment...
A. parr(parr's value) == &parr(parr's address)
(reference = 1 & 9)
B. parr == curr_point, except that parr only point to the first element ONLY, but curr_point iterate through each element in the array
(reference = 1 & 2 / 4 & 6/ 5 & 7)
note: which what I mean by "const" of in the attachments...
C. str_iteration is similar to the parr, except it doesn't have its own value(str_iteration can't be compiled) and the value of pointer's pointer's value.(**str_iteration can't be compiled too)
D. the curr_points value, which holds address of each element in the array, and some of the address actually come's from:-
- &str_iteration
- &curr_points
- &parr
note, see the "highlighted" part =)
(reference = 2)
E. curr_points has it own address which is seperated from its element's address, thought so based on D., some element use the curr_points's address as its own...
(reference = 10)
conclusion and wonders:
1- element of a vector/array can "share" same address with the vector/array it was put into. (Well, I always thought each value has its own address)
(reference = 2)
2- from what I learned, when we setup an array, the pointer will referred to the first element automatically, and that's what (I thought) happen on the pointer's pointer's value(**parr and **curr_points)
(reference = 5 / 7)
3- based on '2-', can anyone give an idea on how to iterate through the pointer's pointer's value? (does ++**curr_point works?) :P
4- from this analysis and other experience with programming C++, I found out that array's format similar to pointer's format, especially when dealing with dynamic allocated array, and I find it confusing at time...
(parr is array, but why it work like curr_point in this test, except that its value == its address(reference A.) while, curr_point's value is referring to the element's address?!
I'm a self-taught programmer, so all of this analysis just come from myself, I need confirmation and solving some of confusion related with these case before I move to multidimensional array topic, hop anyone can help =)