hi all ;)
i'm fairly new with c++ and i'm having some problems with a dynamic array of structures.
I've declared the array with the syntax
structname *pointername= new structname[3].
Now, if i try to assign to one of the structures members a value the compiler (visual c++ 2003) gives me an error.
I've tried with these syntaxes:
structname->membername=
(*structname).membername=
(structname).membername=

with the first two syntaxes the compiler gaves me an error (with the first it says that the struct doesn't have a -> operator, with the second it says something like it isn't a struct).
With the third syntax the program works a little.. if i try to assign something different from a string the program works, otherwhise it gaves me this error: cannot convert from 'const char[number of chars in the string]' to 'char[max size of the string].
Can anybody tell me what i'm doing wrong? :o
Thanks for your help, bye

p.s. sorry for my english ;)

Recommended Answers

All 3 Replies

structname is the type. So what the compiler is telling you is that you are trying to do something akin to int = 5. You want to assign to a member of the object pointed to:

#include <iostream>
using namespace std;

struct structname
{
   int i;
};

int main(void)
{
   structname *pointername = new structname[3];
   pointername[0].i = 42;
   cout << pointername[0].i << endl;
   return 0 ;
}

The array notation dereferences the pointer, so the . operator is the one to use.

thank you for your help i didn't know that arrays deference pointers ;) , i have to sign that somewhere :)

the problem with the convert error was that i had to use strcopy instead of a simple assignment.

i didn't know that arrays deference pointers

It's tied to the equivalence of a and *(a+i).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.