Structures and Ptr->next

This is a discussion on Structures and Ptr->next within the C Programming forums, part of the General Programming Boards category; I have the following code that outputs a consistent address for the Head Pointer, but it also outputs a consistent ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    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*/

    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    11

    Unhappy

    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.
    Be HAPPY

  3. #3
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    When you copy a string you need to use strcpy rather than assignment. Seems like you are using so many pointers that you might want to create an array of them. I've build simpler linked lists.
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  2. Structures tutorial problem
    By Steve MacD in forum C Programming
    Replies: 3
    Last Post: 11-21-2005, 04:27 PM
  3. Modifying info on Exe & Dll's
    By johnd in forum Windows Programming
    Replies: 9
    Last Post: 11-16-2003, 12:49 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  5. need help with data structures
    By straightedgekid in forum C++ Programming
    Replies: 0
    Last Post: 10-05-2001, 03:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21