Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with C++ pointers syntax very new to me

Posted on 2004-10-27
17
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

610 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