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. #1
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    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. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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
    Last edited by FillYourBrain; 07-23-2003 at 11:43 AM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    161
    FYB, you forgot a '&':

    Code:
    void something(int *X, int Z)
    {
    *X = *X + Z;
    }
    
    
    int x = 1;
    int y = 2;
    something(&x,y);

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    oops. Fixed it
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43
    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. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    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. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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. #8
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    THANK YOU

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

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    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. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    is Mary hot?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    Is Mary Hot?

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

  12. #12
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    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
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21