Archived

This topic is now archived and is closed to further replies.

TYgl_88

pointers, structs, and parse errors

Recommended Posts

I''ve got a program that has two structs. In on struct I have several variables and in the other struct I have an array of the first and a pointer to the next. I also have a linked list of the second struct, thus the need for a next pointer. Latter on in the program I have a function that alocates memory for a new node of the second struct. I than try and intialize the array in the newly allocated struct before I send it to be added to the list. The problem is I keep getting parse errors. Here''s a simplier case: #include <iostream.h> #include <stdlib.h> struct FirstStruct { float variable; float secvariable; }; struct SecondStruct { FirstStruct array[1]; }; int main() { SecondStruct* pt = new SecondStruct; pt->array[1]={{1, 1}};//***Parse error occurs here*** system("PAUSE"); return 0; } Any suggestions? Thanks in advance.

Share this post


Link to post
Share on other sites
Indexes begin with the number 0!

pt->array[0]

Secondly, the '{ }' does not work inside code, but only on initialization of static arrays.

You'll have to do it this way:

pt->array[0].variable =1;
pt->array[0].secvariable =1;


I'm not quite sure what you're trying to do (linked list?), but an array of size 1 is not really an array. At least, there's no need to make it an array if it only contains one entry. And, where's the next pointer you're talking about?

[edited by - Wildfire on November 16, 2003 11:11:16 AM]

Share this post


Link to post
Share on other sites
And don't forget to delete pt!
Also...I'm sure you already know that instance of classes can be created on the stack just like int, etc.—you don't need to use new unless you're dependent upon information only known at runtime to instantiate it.

[edited by - merlin9x9 on November 16, 2003 11:11:23 AM]

Share this post


Link to post
Share on other sites
Thanks for the replies. I think I get it now. Also while it may seem like I don''t need to use a linked list in the grand scheme of things I do; the above program was just a simplier case yeilding the same parse error. That''s why it doesn''t have the next variable and such. Anyhow, thanks again.

Share this post


Link to post
Share on other sites