Let's say you have a structure definition struct { int x; } a[10]; . For the sake of indexing, a is a pointer, so you can get to an element through either the a[<index>] or *(a + <index>) methods. At that point you have a structure object, so the . operator is used:
a[2].x; // This works
(*(a + 2)).x; // This works too
You can do indexing to get the address as well, in which case you'd have a pointer to a structure object:
(&a[2])->x; // This works
(a + 2)->x; // So does this
In your tests you were digging too deep as far as levels of indirection, assuming this is an array of struct objects rather than an array of pointers to struct objects:>pt[2]->structureElement
p[2] is presumably a structure object, not a pointer to a structure unless your struct looks like struct { T structureElement; } *pt[N]; . So instead of using the arrow operator, which only works on pointers to structures, you use the dot operator:
pt[2].structureElement
>(*pt[2]).structureElement
Once again, pt[2] is already a structure object, not a pointer, so you can't dereference it.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
The array-index operator is actually a type of de-referencing operator, like * or ->, that's specific to arrays. The operator has lower operator precedence than the other de-referencing operators, so it behaves strangely in these situations.
You need to get to the element before you try to dereference it. Have you tried (pt[2])->element ?
Otherwise, have you done any "pointer arithmetic" yet? The array index operator is actually syntactic sugar. Pointer arithmetic is the underlying behavior that makes the array index operator work. That will probably be your easiest way to do it.
customType *myCustomObjs[2] = {new customType, new customType};
*(myCustomObjs + 1) -> structElement;
EDIT:
Where'd Narue come from???
@Narue:
The OP needs to use the new operator...
"The question at the end of the chapter that I'm working on wants me to create an array of structures and to initialize the array of structuresusing the new function." (emphasis added)
Maybe I'm mistaken, but doesn't this imply that the array should be an array of pointers.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
The new operator returns a pointer (address) of the object(s) that memory has been allocated for. That pointer must be stored someplace in order for it to be gain access to the memory. When the [] operator is used to allocate memory for a group of objects then memory will be consecutive and the address that is returned will be the address of the first object in the group of objects. It turns out that the name of an array is a pointer to the first element in the array so it turns out that the pointer to which the new operator an address to will be able to act like an array, even though technically it isn't an array. That means you can use the [] operator on the object just like you would normally.
candybar *snack = new candybar [3];//allocate memory for three candybar objects, or, eqivalently, declare memory for an array of 3 candybar objects and call the array snack.
strcpy(snack[0].name, "Snickers"); //place Snickers in the name field of the first element in snack.
Once the memory has been allocated using the new or new [] operators you can't initialize it with this syntax:
snack[0] = {"Mocha Monch", 2.3, 350};
You have to assign each field separately and since you can't assign C style strings one to the other, you must use strcpy().
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396