dynamic array of structures problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 2
Reputation: TheWorld is an unknown quantity at this point 
Solved Threads: 0
TheWorld TheWorld is offline Offline
Newbie Poster

dynamic array of structures problem

 
0
  #1
Feb 26th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: dynamic array of structures problem

 
0
  #2
Feb 26th, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 2
Reputation: TheWorld is an unknown quantity at this point 
Solved Threads: 0
TheWorld TheWorld is offline Offline
Newbie Poster

Re: dynamic array of structures problem

 
0
  #3
Feb 27th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: dynamic array of structures problem

 
0
  #4
Feb 27th, 2005
Originally Posted by TheWorld
i didn't know that arrays deference pointers
It's tied to the equivalence of a[i] and *(a+i).
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC