-
Structures and Ptr->next
I have the following code that outputs a consistent address for the Head Pointer, but it also outputs a consistent address for the Ptr. The Ptr->next actually points to a new address each time, but for some reason, the BPtr = BPtr->next won't reassign it.
This is part of a program that reads from a .dat file to assign members of a structure.
Could someone please look at this and see what I've done incorrectly?
Thanks,
readerwhiz
===
{
BOOKPTR BPtr=NULL;
BOOKPTR BTmp=NULL;
BHead = (BOOKPTR)(malloc(sizeof(struct Book)));
BPtr = BHead;
while ( fgets(buffer, sizeof(buffer), Ptrbook) != NULL)
{ /*open while*/
char *num = strtok(buffer, "\t");
char *name = strtok(NULL, "\t");
char *author = strtok(NULL, "\t");
char *pub = strtok(NULL, "\n");
if (buffer != NULL)
{
BPtr->Number = num;
BPtr->Title = name;
BPtr->Author = author;
BPtr->Publisher = pub;
BPtr->next = (BOOKPTR)malloc(sizeof(struct Book));
printf("%x\n%X\nNumber: %s\nTitle: %s\nAuthor: %s\n%X\n",
&BHead, &BPtr, BPtr->Number, BPtr->Title, BPtr->Author,
&BPtr->next);
printf("===\n");
BPtr = BPtr->next;
} /*close if buffer*/
} /*close while*/
}
-
Just check BOOKPTR is declared as
typedef struct Book *BOOKPTR;
Also check whether BHEAD is declared properly???
Try changing the compiler !!!!! Just give it a try .
I think everything else is OK.
-