# pointer question

This is a discussion on pointer question within the C++ Programming forums, part of the General Programming Boards category; Hi , I am still confused about pointers. I have a simple function ,that does some math calculations, called void ...

1. ## pointer question

Hi ,

I am still confused about pointers. I have a simple function ,that does some math calculations, called

void something( int Y, double *Z)
{
Y = Y + Z }

I want to call it in main, but am confused as to how the *Z should be called

I know it can't be called like :

something (3, 4);

it gives an error of:
"passing double to argument 2 of something(int, double *) lacks a cast"

But, I am unclear of how casting works and what its for, and not sure how this "double *Z" argument works.

Thanks for any explanations!!! It is appreciated.

2. are you trying to use Z as some kind of return value? You aren't changing Z so I'm pretty confused about that function. You can't pass in a constant like that and expect it to just work as a pointer either.

void something(int *X, int Z)
{
*X = *X + Z;
}

int x = 1;
int y = 2;
something(&x,y);

would work though

3. FYB, you forgot a '&':

Code:
```void something(int *X, int Z)
{
*X = *X + Z;
}

int x = 1;
int y = 2;
something(&x,y);```

4. oops. Fixed it

5. Sorry about that,

the function is essentially:

void something(int X, double *Z) {

X = X + (*Z) }

thanks ...I tried what you both suggested and it did work. I am still fairly unfamilar with pointers and trying to figure out how this function works.

I'm sorry for the simple question, but could you explain how the function and the call in main are working, in reference to the double *Z only??

Thanks for your help and explanation!!

6. am still confused about pointers. I have a simple function ,that does some math calculations, called

Code:
```void something( int Y, double *Z) {
Y = Y + Z
}```
you're passing an int and a double pointer to something(). To access what Z points to, dereference it:

Code:
```void something (int Y, double* Z) {
Y = Y + *Z;
}```
The problem here is that Y is a new copy in the something function. Y will disappear when your function exits. To use another int from before this function, use a reference:
Code:
```void something (int& y, double* z) {
y = y + *z;
}```
You might as well use a reference for z, too, since there's no reason to prefer a pointer here.
Code:
```void something (int& y, double& z) {
y += z;
}```
a += means add what's on the right to the left variable. it's shorter, so people like it over y = y + z.

7. A pointer is an address. Your function is expecting an address, not a constant like "4". In general, you don't know or care what the address is, so you use the & operator. And, it's safer to let the computer figure it out.

The error message:
Casting is short for typecasting. It means converting one variable type to another. i.e converting a char to an int. In this case, the compiler is telling you that it can't automatically change the constant 4 into a pointer for you.

Compilers often get confused because they don't know what you actually intended. So, sometimes the error messages are misleading.

8. ## THANK YOU

Thanks for the explanations!!, I understand pointers a bit more

9. ## My standared pointer analogy:

If Mary has the answers to the homework, she's a normal variable.

If Mary's a pointer, then she doesn't have the answers. She has the address of the person with the answers. You can use Mary= to get the address. Or, you can use *Mary= to make her bring you the answer.

One use for pointers...
I would say that pointers are most often used when you want get around the fact that functions can only return one value. If you pass-in pointers to variables, then you can change the actual variables. For example, functions that affect character strings use pointers.

10. is Mary hot?

11. ## Is Mary Hot?

Mary must be hot. I think that she is a hot blonde since she does'nt have the answers.

12. Mary must be pretty hot.
Code:
`mary = NULL; //go home mary`
I guess she forgot the address huh?

JK

Well, here is how I think of pointers.

Pointers are just like ints. Except the number inside is a SPECIAL number. It is a number to some place in memory. And you can actually play with those numbers but usually when you do, bad things happen.
Now, what can you do with that special number???
You can see where the special number is going to or what it is
pointing to. Using '*'.
Well now, how can I make a pointer point to something that i want to point it to. Well, we have another operator... '&' and that will give you the special number (address) of any other variable you want.
EX:
Code:
```int *ptr=NULL; //this is just like an int, except that the number it holds, is an address location
int regular; //this is a regular integer
regular = 42;
ptr = 42; //you CAN do this, but when it runs, and you try to see where it points... it points to a FORBIDDEN ZONE
ptr = 0x8665BA32; //is probably better than the one before, but if you don't know what is at that address and if it will remain there... its bad.
ptr = regular;  //isn't this the same thing as ptr = 42?
*ptr = regular; //this means that your changing what ptr points to.  And your changing that place a 42.
ptr = &regular; //ah- there we go.  (read as: the ADDRESS OF regular is assigned to ptr)```
for a more in-depth lesson...

http://cslibrary.stanford.edu/102/PointersAndMemory.pdf

If you're still having trouble, draw a diagram of boxes and on top of the boxes put the name of the variable. In the box put the value of the variable but for pointers, draw an arrow to another box.

Hope this helped some.

-LC

Popular pages Recent additions