954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

dynamic array of structures problem

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[i]->membername=
(*structname[i]).membername=
(structname[i]).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 ;)

TheWorld
Newbie Poster
2 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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.

TheWorld
Newbie Poster
2 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 
i didn't know that arrays deference pointers

It's tied to the equivalence ofa[i] and *(a+i).

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You