# trying to understand pointers. 2 examples.

This topic is 2867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone. I have just started learning about pointers and references in one of my books. I keep getting weird results each time I try and compile this program. First, ill run it and it works. I run it again and then it gives me errors and points me to the pointer. Each time I ran it, it did it again and again. Worked then didn't. Worked then didn't. Can you see if its my code or if its my compiler? All its doing is returning two random dice values using pointers. void rollDie(int* die1Ptr, int* die2Ptr); int main(array<System::String ^> ^args) { srand(time (0) ); int die1 = 0, die2 = 0; rollDie( &die1, &die2 ); cout << die1 << ", " << die2; system("PAUSE"); return 0; } void rollDie(int* die1Ptr, int* die2Ptr) { *die1Ptr = rand() % 7; *die2Ptr = rand() % 7; } In the Class Stack it says pointers.exe!rollDie(int* diePtr = 0x0015EE68, int*die2Ptr = 0x0015EE6c)

##### Share on other sites
I don't see anything weird in your pointer code (thinking it was normal C++) ... but then I noticed the strange main signature ... so you are obviously using C++/CLI with the .NET extensions turned on - because that is a .NET main signature, not the normal C++ version. Although I haven't seen anything in your code that should be broken by running in .NET mode, I would have to dig deeper to be sure.

##### Share on other sites
Im still learning myself but isnt your mistake that your function takes pointers as an argument but you are infact putting in a reference?

Shouldnt it be a little like the following?

void rollDie(int* die1Ptr, int* die2Ptr);int main(){srand(time (0) );//variablesint die1 = 0, die2 = 0;//variable pointersint* pDie1 = &die1;int* pDie2 = &die2;rollDie( pDie1, pDie2 );cout << die1 << ", " << die2;system("PAUSE");return 0;}void rollDie(int* die1Ptr, int* die2Ptr){*die1Ptr = rand() % 7;*die2Ptr = rand() % 7;}

p.s im still very much a noob and have not tested this code. But I think im right. I hope this helps.

Kind Regards
David

##### Share on other sites
Quote:
 Im still learning myself but isnt your mistake that your function takes pointers as an argument but you are infact putting in a reference?
No, there are no references in the OP's code. (Also, based on a quick look at least, your code is functionally equivalent to what the OP posted.)

##### Share on other sites
try replacing your random number generator code with:

(have to include : <ctime>)
time_t t;
time(&t);
srand(t);

to use it just do what you where doing before:

variable = rand() % value_here

Don't know if this'll help, but i couldn't find anything wrong with your code..

##### Share on other sites
Define "gives you errors".

Also:

What values do you think can possibly result from doing 'rand() % 7'? How many different values is that? How many sides does a normal die have? Is this what you want the code to do?

(Answers: (a) 0,1,2,3,4,5 and 6; (b) that makes seven in total; (c) six; (d) probably not.)