C++ Workshop - Pointers (Ch. 8)

Started by
87 comments, last by mihaimoldovan 11 years, 11 months ago
Hello all,

That is correct, once again I have a question, lol. In the following code, I was practicing how to use New and Delete with Pointers. In my little while loop, LocalVariable++ does exactly what I thought it would do, increments by 1. However, the next line *pHeap +=1, I had originally put in *pHeap++ thinking it would do the exact same, increment by 1....however, that did not happen. Instead I got all kinds of weird values for *pHeap and a Debug Assertion Failed error. Now my understanding is that using the * in front of the variable gives me the value at pHeap, correct? Why then would a simple ++ not work?

Thanks,

Shawn

*Edit* What is a debug assertion fail error anyhow? lol, i guess i can google that, evidentally it is something bad, ;).

//Using New and Delete and Pointers#include <iostream>int main(){	using namespace std;	unsigned short LocalVariable = 0;						//creates a local variable in main	unsigned short *pLocalVariable = &LocalVariable;		//creates a pointer to LocalVariables address	unsigned short *pHeap = new unsigned short;				//creates a pointer on the heap(free store)	*pHeap = 1;			//assigns the value 1 to the heap variable pHeap	while (LocalVariable<10)	{		cout<<"LocalVariable = "<<LocalVariable<<endl;		cout<<"*pLocalVariable = "<< *pLocalVariable<<endl;		cout<<"*pHeap = "<< *pHeap<<endl;		LocalVariable++;		*pHeap += 1;	}	delete pHeap;	return 0;}


[Edited by - shawnre on June 3, 2007 11:27:17 AM]
Advertisement
(*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.
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.

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