Sign in to follow this  
marky03

trying to understand pointers. 2 examples.

Recommended Posts

marky03    122
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 this post


Link to post
Share on other sites
Xai    1838
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 this post


Link to post
Share on other sites
bentaberry    100
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) );

//variables
int die1 = 0, die2 = 0;

//variable pointers
int* 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 this post


Link to post
Share on other sites
jyk    2094
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 this post


Link to post
Share on other sites
Waaayoff    952
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 this post


Link to post
Share on other sites
Zahlman    1682
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.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this