Free2Code
The majority of forums are now only available as archives, which means posting/editing is disabled.

The Anything and Everything forum is still open.
 
Time: 2013-05-19, 06:58am
Dynamic arrays of structures, operator usage
Subject: Dynamic arrays of structures, operator usage  ·  Posted: 2006-05-05, 06:49pm
Rank: ? (2)
Member #: 27416
Just a quick question. I'm trying to creat and populate a dynamic array of structs, but the compiler is not reacting as I would expect. I'm using Dev-C++.

Code:
  1. #include <iostream>
  2. #include <string>
  3. struct Chips
  4. {
  5.        std::string name;
  6.        double weight;
  7.        int calories;
  8. };
  9. int main()
  10. {
  11.     using namespace std;
  12.     Chips *snacklist = new Chips[3];
  13. ...


after this, I would expect the operator to be used to populate each pointer to be ->, but when I try this...

Code:
  1. snacklist[0]->name = "Doritos";


the compiler returns
16 D:\...\chips.cpp base operand of `->' has non-pointer type `Chips'

If I switch over to the . operator, the compiler goes without a hitch, but this leads me to believe that the program isn't working off of pointers, thus making me question whether the memory is being properly allocated with 'new'. Can anyone clue me in as to what's going on here?

Also, as an aside, is there code to initialize a structure within a dynamic array, or do I have to populate each pointer location individually?

Thanks,
DT

» Post edited 2006-05-05, 06:54pm by Devilturnip.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Dynamic arrays of structures, operator usage  ·  Posted: 2006-05-06, 02:39am
Rank: ? (768)
Member #: 11085
When you create a dynamic array of structures or classes, using the offest operator ([X]) makes it so that you should use . and not ->

- relpats_eht
 
  Reply to this ·  Post link ·  Top
Subject: Re: Dynamic arrays of structures, operator usage  ·  Posted: 2006-05-06, 04:31am
Rank: ? (2)
Member #: 27416
So everything is operating right with the . operator? Muchas gracias.

 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

Penguino AVR

Want to learn about robotics or microcontrollers?
Check out the Penguino AVR from our friends at
Icy Labs