Advertisement

C++ Workshop - Pointers (Ch. 8)

Started by July 17, 2006 03:19 PM
87 comments, last by mihaimoldovan 12 years, 9 months ago
(*pHeap)++; --> Order of precedence.

OR

(*pHeap) += 1; can be used aswell.
Ah yes, thanks JOBe, now that you pointed that out, I recall reading that, and upon review, that is now clarified.

Thanks,

Shawn

*Edit* Well, the *pHeap +=1 seemed to work fine in this example. Maybe there is something I am still not grasping here with respect to pointers...let me try your first suggestion to see what happens then.
Advertisement
JOBe, thanks again, seemed the (*pHeap)++ worked fine also. So, I wonder if I should have used the dereference within () for the +=1 part or not....seemed to work either way.
Quote:
Original post by shawnre
JOBe, thanks again, seemed the (*pHeap)++ worked fine also. So, I wonder if I should have used the dereference within () for the +=1 part or not....seemed to work either way.


Hello shawnre,

As I said earlier, the precedence for postfix is higher then the dereference operator you are using. In your original code, you where altering the address rather then the value.

When using *pHeap += 1; or (*pHeap) += 1; the dereference has higher precedence. Which one to prefer? Don't know really, but, I would prefer to use the safest of the two. (*pHeap) += 1;

Actually, I really prefer (*pHeap)++;

Hope this helps.

Oh, yes, Debug Assertion Failed error == NOT GOOD [SMILE]
I thought the classes bit was hard, I had to read through half the stuff in this chapter twice before I fully got it.

Anyways if this is the hardest chapter in the book I should do fine the rest of the way.
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life
I'm not sure I fully understand pointers

if
a='z'
b=&a
c=&b
then to get c to return 'z' I need to append ** to c (c** == 'z')
if I simply used c* it would return the address of b rather than the value of a.

I have seen people use

*type varname
type* varname
type *varname
type varname*

could someone please explain each of these

also can I use
*type varname*
type* *varname
*type *varname
type* varname*

and what would this mean

I hope I have not put anyone in a whirlwind like myself

If I'm an idiot. Please forgive me, and explain how and why so I can learn.

Advertisement
Well, i found this a good read, and even though this question was asked a month ago or so, I thought I'd try and answer it...

Quote:
Original post by lewc
I'm not sure I fully understand pointers

if
a='z'
b=&a
c=&b
then to get c to return 'z' I need to append ** to c (c** == 'z')
if I simply used c* it would return the address of b rather than the value of a.


I think it may seem more confusing without the context of the types of a, b, and c.

char a = 'z';
char* b = &a //Pointer to a
char** c = &b//Pointer to pointer to a

To find 'z' down the pointer-trail from c you would use:

char a0 = *b; // or
char a1 = **c; // a1 == a0 == a == 'z' after this line is executed

Not so confusing, huh?

Quote:
Original post by lewc
I have seen people use

*type varname
type* varname
type *varname
type varname*

could someone please explain each of these

also can I use
*type varname*
type* *varname
*type *varname
type* varname*

and what would this mean

I hope I have not put anyone in a whirlwind like myself


Unless there's some kind of special case I'm not aware of, lines that consist of only this: *type varName; or this: type* varName*; are always syntax errors. The * before type in the first one has no meaning, nor does the * after varName. I'm sure something horrible could be brewed up using operator overloading and pre-processor stuff, but.. whatever.

The others you listed are simple to grasp once you get used to the fact that this:
char* pChar; //is the same as:
char *pChar; //and also the same as:
char*pChar; //and even the same as:
char
*
pChar; //They all just say that pChar is a pointer to a char.

so for your last example:

type* *varname;

varname is a pointer to a pointer to an instance of type, just like:
char** ppChar; //which is clearly a pointer to a pointer to a char

HTH :D

*type varname
type* varname
type *varname
type varname*


1. doesn't exist
2. pointer to an object of type 'type'
3. same as 2, with emphasis on the pointer binding to that specific variable (if you have more than 1 per line, not recommended)
4. doesn't exist

This topic is closed to new replies.

Advertisement