Solved

Help with C++ pointers syntax very new to me

Posted on 2004-10-27
217 Views
Last Modified: 2010-04-01
Hello everyone, how are you doing? I am working with pointers in c++ and I have not done anything at all with them so I am learning. I have a struct with some datatypes and then the struct name with some pointers. In one of my functions I am trying to make this happen
y pointing to the root of Tree. Basically what I am asking is if I need a pointer that is not part of my to equal the null pointer of my Tree. How would I declare or syntax that. I have tried y=T->NULL but it gives me an constant compiling error. Probably because I am decalring why as pointer to my struct. How else should I decalre this? Thanks for the help

0
Question by:D_basham
    17 Comments
     

    Author Comment

    by:D_basham
    Actually I would like to ask another question really quick that is realated. Disregard last quesiton I figured out the answer. When I compile it gives me warnings on my node ptr not being initalized why is that what should a pointer be initalized to, NULL?
    0
     
    LVL 11

    Accepted Solution

    by:
    I am not sure I understand your question but:

    struct TreeNode {
      int data;
      TreeNode *left, *right;
    };

    We have a type called TreeNode. It has pointers at other TreeNodes.

    TreeNode *root;

    Now we have a variable (called root) that points at a TreeNode. To initalize root to NULL (or 0), we just assign it:

    root = NULL;

    At some point you want to point y at the root of your tree. That is done with

    TreeNode * y = root;

    Hope this helps,
    -bcl
    0
     

    Author Comment

    by:D_basham
    And yest that helps. That is what I was doing I just wasn't sure I was on the right track. Thank you

    I am looking at the specs of this it is. Is parent and root pointer be considered the same thing?

    0
     
    LVL 11

    Expert Comment

    by:bcladd
    Probably not. A tree has one root (and a pointer that points at it) and every node has a parent pointer (a pointer at the node that it is the left or right child of). How you handle the parent of the root node is implementation dependent (could be NULL, for example).

    -bcl
    0
     

    Author Comment

    by:D_basham
    I am sorry but if you could answer one more question I know that this is not a lot of points. I am stuck on this one.

    I understand what your saying how could I in syntax have y<-null[root pointer]? Thanks
    0
     
    LVL 11

    Expert Comment

    by:bcladd
    I do not understand what you want. Happy to answer but I don't understand.

    -bcl
    0
     

    Author Comment

    by:D_basham
    I think I follow this specs. What I am asking how do I get y to be the NULL Root pointer? I think that is what I mean? :)
    0
     
    LVL 11

    Expert Comment

    by:bcladd
    What is a NULL root pointer? A pointer at a node with a NULL child? Two NULL children? I am sorry but I don't recognize your terminology.

    -bcl
    0
     

    Author Comment

    by:D_basham
    Maybe I am not understanding what I am reading. Let me explain what I am seeing. There is a function that has parameters (var_1(root pointer), var_2(new node root pointer) then inside the the function is
    y<-(this is an arrow) nil(which is NULL)[var_1(root pointer)]. I can't follow what this spec is saying. Doe this help? Are is still not clear? Thanks
    0
     
    LVL 11

    Expert Comment

    by:bcladd
    Can you post the whole function? What is the function supposed to do. I understand <- to mean assignment but [] off of a NULL (or nil) pointer doesn't make any sense to me.

    Or, if you can't post the function, can you find a pointer to it on the Web? Amazon lets you search inside of books...

    -bcl
    0
     

    Author Comment

    by:D_basham
    I guess I should read a little further through it. what I think it is saying is a var_1 = NULL pointer to the root. Does that make since. Sorry I couldn't find anything.  
    0
     
    LVL 11

    Expert Comment

    by:bcladd
    What is the function called? Just curious now.
    -bcl
    0
     

    Author Comment

    by:D_basham
    Insert()?
    0
     
    LVL 11

    Expert Comment

    by:bcladd
    Okay. Are we inserting into a binary search tree (in order) or just some arbitrary tree?

    Is the function recursive (does it call the same version of the function from within the function body) or not?

    -bcl
    0
     

    Author Comment

    by:D_basham
    Oh the before page, of the function is stuck together it is A Red and Black Tree. So it should be a NULL pointer for all nodes correct?
    0
     
    LVL 11

    Expert Comment

    by:bcladd
    I don't have my data structures book here with me and rb-tree insertion is not one I tend to have in memory.

    A presentation of an insert routine is available at http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/red_black.html

    Hope it helps,
    -bcl
    0
     

    Author Comment

    by:D_basham
    Thanks that will help. This isn't worth a whole lot of points but I think I have a better idea on what I need to do on this program from your first response. Thanks.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now