473,385 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pointer convension, dot vs. indirection notation

In a recent posting, my notation was corrected:
tree* prootNode = new tree;
tree* psubNode = new tree;

prootNode->child = psubNode;
psubNode->parent = prootNode;


struct tree rootNode;
struct tree subNode;

rootNode.child = &subNode;
subNode.parent = &rootNode;

Is this due to some general C++ convension/practice or because its
compiler specific to use indirection? I've seen many compilers
understanding -> however.

Thanks,
Casper
Jul 22 '05 #1
4 2331
> In a recent posting, my notation was corrected:
> tree* prootNode = new tree;
> tree* psubNode = new tree;
>
> prootNode->child = psubNode;
> psubNode->parent = prootNode;
struct tree rootNode;
struct tree subNode;

rootNode.child = &subNode;
subNode.parent = &rootNode;


I would event say

tree rootNode;
tree subNode;

The struct qualifier is redundant.
Is this due to some general C++ convension/practice or because its
compiler specific to use indirection? I've seen many compilers
understanding -> however.


There is a difference between these two : prootNode and psubNode are
pointers to heap-allocated objects and rootNode and subNode are objects
allocated on the stack. If you don't know the difference between them, you
should by a better book. The general practice is : if you don't need to
allocate objects on the heap, put them on the stack. It is faster, more
efficient (and all the good words like that) and easier to work with. No
news is good news.
Jonathan
Jul 22 '05 #2
On Sat, 14 Aug 2004 15:43:04 -0700, Casper <ca****@jbr.dk> wrote:
In a recent posting, my notation was corrected:
> tree* prootNode = new tree;
> tree* psubNode = new tree;
>
> prootNode->child = psubNode;
> psubNode->parent = prootNode;


struct tree rootNode;
struct tree subNode;

rootNode.child = &subNode;
subNode.parent = &rootNode;

Is this due to some general C++ convension/practice or because its
compiler specific to use indirection? I've seen many compilers
understanding -> however.


I think you misunderstand. I didn't see the original post, but your code
and the correction are just different code. Your code uses pointers to
structs (hence ->) and the correction uses structs (hence .). These are
just different, it's not a case of one being correct notation and the
other not. I can't say which is right for you, but at a guess your
original code looks more likely, maybe you misunderstood the point that
was being made.

In any case you better learn the difference between something which is a
pointer and something which is not, and when you would use one and when
the other, if you are going to be programming dynamically allocated trees.

john
Jul 22 '05 #3
John Harrison wrote:
I think you misunderstand. I didn't see the original post, but your
code and the correction are just different code. Your code uses
pointers to structs (hence ->) and the correction uses structs (hence
.). These are just different, it's not a case of one being correct
notation and the other not. I can't say which is right for you, but at
a guess your original code looks more likely, maybe you misunderstood
the point that was being made.

In any case you better learn the difference between something which is
a pointer and something which is not, and when you would use one and
when the other, if you are going to be programming dynamically
allocated trees.

john


My original post was about the lifespan of structs as tree nodes. It
would appear I did not understand the context of the point that was
being made, which was in regard to a root node only. It did not make
much sence to me as you would have a hard time building a dynamic tree
using structs on the stack only.

Because if I am allocating (sub)nodes dynamically and adding to my tree,
I might as well have the root be on the heap/free store as well. Thanks
for clarifying!

Casper
Jul 22 '05 #4

"Casper" <ca****@jbr.dk> wrote in message
news:5V********************@weber.videotron.net...
In a recent posting, my notation was corrected:
> tree* prootNode = new tree;
> tree* psubNode = new tree;
>
> prootNode->child = psubNode;
> psubNode->parent = prootNode;


struct tree rootNode;
struct tree subNode;

rootNode.child = &subNode;
subNode.parent = &rootNode;

Is this due to some general C++ convension/practice or because its
compiler specific to use indirection? I've seen many compilers
understanding -> however.


There is nothing wrong with ->. I don't know the context of your original
code, but it might have been "corrected" because the general rule is don't
use dynamic memory allocation unless you really need to.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Matt Taylor | last post by:
I was a bit surprised when my compiler (both GCC 3.2 & MSVC 7.1) griped about this piece of code: int i; int *p1 = &i; const int **p2 = &p1; // Invalid type conversion here I was curious as...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Timo | last post by:
I am trying to pass a pointer to an array of structures to function, but I have some problems. I am using MS Visual C++ 6.0, but I think this is more of a C-problem than Windows programming...
7
by: rs | last post by:
Just out of idle curiosity: 1)Regarding pointers; I don't have the standard/grammar with me, but T* for type T mean pointer-to-T. Does that mean that with a declaration of type T** is actually...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
3
by: Immortal_Nephi | last post by:
Sometimes, unsigned char array is located in the file scope. You define A Object and B Object. A Object and B Object need to share unsigned char array. They are very different object. They are...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.