| | |
dynamic array of structures problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 2
Reputation:
Solved Threads: 0
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

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
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: The array notation dereferences the pointer, so the . operator is the one to use.
#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 ;
} "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
•
•
•
•
Originally Posted by TheWorld
i didn't know that arrays deference pointers
"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
![]() |
Similar Threads
- Dynamic array of structures (C++)
- Dynamic Array Help (C)
- Dynamic Array of Structures, Loop problem! (C++)
- Creating dynamic array structures (C++)
Other Threads in the C++ Forum
- Previous Thread: I need help with C++
- Next Thread: I need help with this particular c++ problem
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







