JE> OK, I am having a problem here I keep getting a Bad NULL pointer
JE> assignment of some sort....
JE> any Ideas or comments...
I've got a few.
JE> #include
JE> #define NUM 3
JE> struct monsterlst monsters[NUM];
Note that you've created three monsters. Those monsters are
accessible via monsters[0], monsters[1] and monsters[2].
JE> printf("\nWhat is the name of the monster: ");
JE> gets(monsters[NUM].monstername);
I wonder where the name is going ??? There's no such thing as
monsters[3]. But this has nothing to do with NULL pointers.
JE> printf("\nWhat is %s's HitPoints: ", monsters[NUM].monstername);
JE> scanf(" %d",monsters[NUM].hp);
Ditto.
JE> monster = fopen("c:\tc\bin\monster.dat", "ar+");
Finally, we manipulate a pointer. (Remember that monster is a
pointer to a FILE object.
First off, the odds of this file residing on your system are quite
remote. The file name would have to lead off with a tab, then a 'c',
then a bell character, then 'in' then ... I don't know what \m would be.
So the odds on bet would be that the fopen() call will not open a
file and monster will be NULL.
JE> if (!monster)
JE> {printf("There was an Error");
JE> fwrite(monsters[NUM].monstername, sizeof(monsters[NUM].monstername), 1
JE> , monster); fwrite(&monsters[NUM].hp, sizeof(monsters[NUM].hp), 1,
JE> monster);
And here we've got some reversed logic. As mentioned fopen() returns
NULL when it shoots craps for whatever reason(s). So the if(!monster)
path is the path taken when the file cannot be opened. And you cannot
fwrite with an unopened FILE*. So that's where your NULL pointer
resides. You're using a NULL pointer as a parameter to fwrite().
---
þ Blue Wave/QWK v2.12 þ
---------------
* Origin: St. Louis Users Group (1:100/4)
|