Re: [Dev-C++] Structures and Arrays and Pointers, Oh My!
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: David M. <ci...@ya...> - 2003-05-09 12:13:04
|
--- OROSZI_Balázs <or...@vp...> wrote:
> David McKen wrote:
> > Ok, I am having trouble using the above 3 together.
> >
> > Slay_msg is a array of pointers to characters (strings)
> > struct pc_data {
> > char * slay_msg[3];
> > }
> >
> > //a pointer to pc_data is a member of the char_data
> struct.
> > struct char_data {
> > ...
> > pc_data * pcdata;
> > }
> >
> > //A pointer is declared of type char_data and
> initalized to the required
> > values.
> > char_data * ch = new char_data; //Assume everything has
> been initalized.
> >
> > My question is: How do I access the strings in the
> array slay_msg?
>
> First of all, this is NOT a pure C program, as you are
> using "new", and
My mistake malloc is used (I just used new for simplicity)
> that is only in C++.
> Do you EXPLICITLY create pcdata inside ch? As it will
> *not* be created.
> This is what I have done, and this works:
>
> int main()
> {
> char_data *ch = new char_data;
> ch->pcdata = new pc_data;
> ch->pcdata->slay_msg[1] = "Dead!";
> printf("%s\n",ch->pcdata->slay_msg[1]);
> delete ch;
> return 0;
> }
>
> Another note: slay_msg is an array of pointers, so you
> can only assign
> values to them, not copy into them!
>
> Access is as follows: ch->pcdata->slay_msg[<index here>],
> what you used!
> Your problem is at creation I guess. You don't initialize
> pcdata I
> think. You must explicitly create it, as it's constructor
> won't get
> called (it is a pointer)!
I see, I will have to go to check that.
> But again, I WARN you! This is *not* pure C! C doesn't
> have "new", and
> structs should be typedefed, so you can create variables
> without writing
> the "struct" word before the declaration. ("struct
> char_data ch;" would
> be needed in pure C)
Those few code snippets are what poped into my head to
explain my problem.
> Try to compile it with gcc, not g++. It won't :)
>
> --
> Greetings,
> Balázs
Cool, This explains alot.
Thank you.
David Mcken
__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com
|