Dereferencing dynamically allocated struct arrays

Jun 21, 2011 at 8:56pm
WLOG, assume structName is a struct with a single int member.

If I dynamically allocate for a single struct, e.g.
structName * pt = new structName;

then I can dereference the members of name as
pt->member = 10;

But if I dynamically allocate an array of structs, e.g.

structName * ps = new structName[3];

then in order to dereference a member of the i-th struct, I must write

ps[i].member = 10;

Short EXAMPLE code
----------------------------------------------------------------
int main()
{
// array case
student * pt = new student[3];
pt[1].age = 38;
cout << pt[1].age << endl;
delete [] pt;

// single struct case
student *ps = new student;
ps->age = 50;
cout << ps->age << endl;
delete ps;
return 0;
}
----------------------------------------------------------------------
Since pt and ps are both pointers, I do not understand why

pt[i]->member = 10;

is incorrect. This seems inconsistent. I can not find a reference to this issue in my textbooks or on the web. I know what is correct, i.e., pt[i].member = 10;

What I want to know is WHY or a reference which provides an explanation. Thanks in advance!



Jun 21, 2011 at 9:50pm
[] does dereferencing for you. pointer[i] is identical to *(pointer+i).
If you want ->, you can write (pointer+i)->something
Last edited on Jun 21, 2011 at 9:51pm
Jun 21, 2011 at 9:53pm
Similarly, you could do pt[0].member instead of pt->member although I think the later makes more sense.
Jun 21, 2011 at 10:38pm
Thank you hamsterman and Mathhead200!

I had found empirically that [] does the dereferencing. Is this a feature particular to C++ struct arrays? I am puzzled that none of my textbooks (Prata, Lafore, Savitch) discuss such a fundamental point, i.e., why pointer[i] ==*(pointer+i), since it is not "intuitively obvious" to me. I found one article in the C++ forum that asserts that [] does the dereferencing, but does not explain why.

For example, I would have thought it equally plausible that pointer[i] is a pointer referring to the i-th struct and not the value of the i-th struct itself - but that apparently is not true. I guess what I am asking is this:

Is pointer[i] ==*(pointer+i) logically compelling or is it simply a C++ shortcut notation?
Jun 21, 2011 at 10:49pm
[] is related to pointers and arrays in general, it has nothing to do with what the pointer points to or what the array contains.
Jun 21, 2011 at 10:53pm
snufflehound wrote:
Is pointer[i] ==*(pointer+i) logically compelling or is it simply a C++ shortcut notation?
It is a C++ shortcut notation... (but only when invoked on pointers!)
Last edited on Jun 21, 2011 at 10:53pm
Jun 21, 2011 at 10:54pm
I think I see it now. This is the same pointer arithmetic concept used with numeric arrays. Is that the answer?
Jun 21, 2011 at 11:02pm
LB wrote:
[] is related to pointers and arrays in general, it has nothing to do with what the pointer points to or what the array contains.
Last edited on Jun 21, 2011 at 11:02pm
Jun 21, 2011 at 11:09pm
I understand it now. Thanks very much.
Jun 21, 2011 at 11:21pm
Just be careful when you see the [] operator being invoked on an object/reference and not a pointer. Examples:
1
2
3
4
5
string s; ...
s[2]; //"s" is not a pointer

vector<int> v; ...
v[5]; //"v" is not a pointer 
In these cases, the operator does whatever the class wants it to do.
Jun 23, 2011 at 4:31am
Good point, thanks again.
Topic archived. No new replies allowed.