Thread: [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 02:52:05
|
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? ch->pcdata->slay_msg[<index here>];seemed logical to me. however I am getting Null pointer assertion errors and nothing is done.when a '&' is placed before the ch it causes a crash. I am about to try putting a '*' before thech though I seriously doubt this will work. N.B. As is this is a pure C program. I would like to leave it that way.
---------------------------------
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo. |
|
From: <or...@vp...> - 2003-05-09 07:49:17
|
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
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)!
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)
Try to compile it with gcc, not g++. It won't :)
--
Greetings,
Balázs
|
|
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
|
|
From: Ioannis V. <iv...@at...> - 2003-05-09 14:25:15
|
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of David =
McKen
Sent: Friday, May 09, 2003 5:52 AM
To: dev...@li...
Subject: [Dev-C++] Structures and Arrays and Pointers, Oh My!
Ok, I am having trouble using the above 3 together.
=20
Slay_msg is a array of pointers to characters (strings)
struct pc_data {
char * slay_msg[3];
}
=20
//a pointer to pc_data is a member of the char_data struct.
struct char_data {
...
pc_data * pcdata;
}
=20
//A pointer is declared of type char_data and initalized to the required
values.
char_data * ch =3D new char_data; //Assume everything has been =
initalized.
=20
Since you use operator new, it is a C++ program. In C you have to use =
the
malloc() family.
=20
=20
My question is: How do I access the strings in the array slay_msg?
=20
ch->pcdata->slay_msg[<index here>];=20
=20
C90:
=20
unsigned long i;
=20
char_data *ch=3Dmalloc(sizeof(*ch));
=20
if(!ch)
{
/* Error handling code */
}
=20
=20
ch->pcdata=3Dmalloc(sizeof(*(ch->pcdata));
=20
if(!(ch->pcdata))
{
/* Error handling code */
}
=20
=20
for(i=3D0; i<3; ++i)
{
ch->pcdata->slay_msg[i]=3Dmalloc(WHATEVER_SIZE_OF_CHARACTERS);
=20
if(ch->pcdata->slay_msg[i]=3D=3DNULL)
{
/* Error handling code */
}
}
=20
=20
=20
This low level stuff in C++98:
=20
#include <new>
=20
=20
// This try block in reality is usually applied in the function call, =
not in
the definition
// but i place it here to demonstrate the whole error handling =
mechanism.
=20
try
{
=20
char_data *ch=3Dnew char_data;
=20
ch->pcdata=3Dnew pc_data;
=20
=20
for(unsigned long i=3D0; i<3; ++i)
ch->pcdata->slay_msg[i]=3Dnew char[WHATEVER_SIZE_OF_CHARACTERS];
}
=20
catch(std::bad_alloc)
{
// Error handling code
}
=20
=20
=20
Ioannis Vranos
* Programming pages: <http://www.noicys.freeurl.com/>
http://www.noicys.freeurl.com
* Alternative URL 1: <http://run.to/noicys> http://run.to/noicys
* Alternative URL 2: <http://www.noicys.cjb.net/> =
http://www.noicys.cjb.net
|