?
Solved

Help with C++ pointers syntax very new to me

Posted on 2004-10-27
17
Medium Priority
?
221 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
Comment
Question by:D_basham
  • 9
  • 8
17 Comments
 

Author Comment

by:D_basham
ID: 12425266
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:
bcladd earned 80 total points
ID: 12425294
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
ID: 12425344
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 11

Expert Comment

by:bcladd
ID: 12425463
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
ID: 12425683
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
ID: 12425713
I do not understand what you want. Happy to answer but I don't understand.

-bcl
0
 

Author Comment

by:D_basham
ID: 12425732
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
ID: 12426547
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
ID: 12426589
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
ID: 12426653
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
ID: 12426710
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
ID: 12426721
What is the function called? Just curious now.
-bcl
0
 

Author Comment

by:D_basham
ID: 12426736
Insert()?
0
 
LVL 11

Expert Comment

by:bcladd
ID: 12426782
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
ID: 12426814
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
ID: 12426841
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
ID: 12426908
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

593 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