944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7979
  • C++ RSS
Feb 26th, 2005
0

dynamic array of structures problem

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TheWorld is offline Offline
2 posts
since Feb 2005
Feb 26th, 2005
0

Re: dynamic array of structures problem

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 27th, 2005
0

Re: dynamic array of structures problem

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TheWorld is offline Offline
2 posts
since Feb 2005
Feb 27th, 2005
0

Re: dynamic array of structures problem

Quote originally posted by TheWorld ...
i didn't know that arrays deference pointers
It's tied to the equivalence of a[i] and *(a+i).
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I need help with C++
Next Thread in C++ Forum Timeline: I need help with this particular c++ problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC