pointers arrrghhhhh

Started by
12 comments, last by Themonkster 21 years, 7 months ago
Though this is a little OT, wouldn''t it be easier just to hand over the integer itself? Pointers seem to be a 4 byte variable outputted in hexdec, and straight ints are either 2 or 4 bytes, depending on implimentation of the system.
Either way, you''d be hitting the same value, but (stunningly enough, this holding true) call-by-value may be more efficient than call-by-reference in this case.
Advertisement
that would be true in this case if the data types are the same size, as for value you''re only pulling the value from the stack and using it, where as with an address you are pulling the address and then having to derefence it to get the value
As Neosmyle said, you really should be returning the value instead of bothing with your pointer.

"To find it again, the program stores the address of the variable on the stack or the heap"

Nope, it just doesn''t work that way. CWizard is right.

Also because this is so dangerous it isn''t legal:

int a = 5;
int *b = a;

To cheat in this way you''ll have to use a cast. Always be very suspicious of casts.
I have to agree with some here that it is better to simply return the value, and instead of

int a;
GetNumber(&a);

use:

int a;
a = GetNumber();

Although, it is not always bad to pass the pointer as you might want the function to return a status code instead. But as you're not using it and return void, it's no point in it.

For example you could have it like this:
int a; while(!GetNumber(&a))    printf("You jerk, I wanted a number! Some people never learn"); bool GetNumber(int *pInt){    printf("ENTER GUESS: ");    return (scanf("%i", pInt) == 1) ? true : false;}  


[edited by - CWizard on September 13, 2002 11:45:28 AM]

This topic is closed to new replies.

Advertisement