pointer to node

This is a discussion on pointer to node within the C++ Programming forums, part of the General Programming Boards category; I know that a pointer must be of the same type or object as the object its pointing to ie ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    41

    pointer to node

    I know that a pointer must be of the same type or object as the object its pointing to ie int pointer points to int,char pointer points to char and so on, But what exactly does a node pointer point to I know that its of type node and therefore must point to a node but it can't point to the first element because if the first element
    is of a smaller type than the second the pointer is of the wrong type for the second element,
    Therefore what does it point to.


    Also just brousing over precedence and associativety fine with precedence but need a quick recap with
    associativety any ideas where I can read up on this matter.

    Thanks all for taking the time to help me.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    the node pointer points to the top of the node structure. The structure is usually just a block of memory with all the parts of the node one after the other. So if it can figure out the top of the structure, the program will know where the other elements are.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Therefore what does it point to.
    A pointer always points to the starting memory location of a block. If an int is four bytes, a pointer to int points to the address of the first byte. If your node is 12 bytes, a pointer to node points to the address of the first of those 12 bytes.

    >need a quick recap with associativety
    Precedence rules tell you how to choose which of several different operators is performed first, associativity rules tell you how to choose which of several of the same operator is performed first.
    Code:
    ex.
      1 + 2 * 3
    
      * has higher precedence, so it is used first.
    
    ex.
      20.0 / 4 / 2
    
      / has left->right associativity, so 20/4 is performed first.
      The answer is 2.5 instead of 10
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I had some time at work so here's an analogy I thought up that might help, maybe, possibly....

    Suppose all the houses on the block of a street were exactly the same construction, meaning they all had a kitchen, a great room, a bathroom and two bedrooms, all with the same floor plan. They were all white and had none of them had shutters or other distinquishing features, externally. In fact even the contents of the house were the same, and includes only a single telephone with an answering machine. In fact the only differences were the address each house had and the family that lived there. Some families had both a mother and a father, some just one or the other. Some had children, some didn't, but at most there could be up to 4 children, because it's only a two bedroom house. Then pretend you can't tell what type of family lives in any given house until you look inside. You're a census taker who needs to determine what type of family lives where. Turns out there are two ways to find out how many people live in the house. You can knock on each door personally, or you can call on the phone by looking up each address in phone directory. Or call up the first phone and listen to the address stored on the answering machine to get the next address.

    Using this analogy, each house is a node. Each node has one data member, an instance of a class called Family, and each Family can have at most one mother, at most one father, both of which are of type bool, and one group of kids which is an array of 4 ints, representing the age of any given child. The phone is a pointer and the answering machine is pointer to the next node in a list of houses stored in the node. The phone directory is an array of phones, that isn't used accept to except to extend the analagy a little.

    Here's the classes to model the situation:
    Code:
    struct Family
    {
       Family() : mother(false), father(false)
        {
           int i;
           for(i = 0; i < 4; ++i)
              child[i] = 0;
        }
       bool mother;
       bool father;
       int child[4];
    };
    
    class house
    {
      public:
        Family family;
        long * nextHouse;//a phone 
    };
    If you knock on the door you use the dot operator to find the information, like this:

    //Declare a house:
    house Smith;
    //fill in family data
    Smith.family.mother = true;
    Smith.family.father = false;
    Smith.family.child[0] = 2;


    if you call on the phone you use the arrow operator like this:
    //declare a phone to a house
    house * SmithPhone = &Smith;
    cout << SmithPhone->family.child[0];

    and to call each family on the block one at a time starting from the first phone in the list you go:
    Code:
    while (phone->nextHouse != NULL)
    {
       if(phone->family.mother)
         cout << "mother" << endl;
       if(phone->family.father)
         cout << "mother" << endl;
       int i, children = 0;
       for(i = 0; i < 4; ++i)
       {
          if(phone->family.child[i] != 0)
               ++children;  
       }
       cout << "and " << children + 1 << " children, ages " << endl;
       for(i = 0; i < children; ++i)
        cout << phone->family.child[i] << ' ';
    
       phone = phone->nextHouse;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 03:05 PM
  3. Memory Leak
    By jtullo in forum C Programming
    Replies: 7
    Last Post: 12-11-2006, 10:45 PM
  4. Need help with C++ linked list using objects
    By pityocamptes in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 11:12 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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