Thread: Pointers and Functions

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    4

    Pointers and Functions

    Question regarding pointers being used in functions..

    Below is some sample code I have written. I understand the principles of points and how they are passed to functions (and used in general).

    However I am unsure exactly as to why when you call a function you omit the * before the argument?

    My thoughts..
    Below I have declared that the argument for function that int a is a pointer (*), therefore when I call the function, I only need to use the variable name and not the specifier * as the function is expecting a pointer??

    Code:
    #include <stdio.h>
    
    
    void function(int *a);
    
    
    int main () {
        int value = 6;
        int *pValue = &value;
        function(pValue);
        
        return 0;
    }
    
    
    void function(int *a) {
        printf("Pointer memory address is %p\n", a);
        printf("The pointer value is %d\n", *a);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,863
    It might be a little more instructive to try this:
    Code:
    #include <stdio.h>
    
    void function(int *a);
    
    int main(void) {
        int value = 6;
        int *pValue = &value;
        int **ppValue = &pValue;
        function(&value);
        function(pValue);
        function(*ppValue);
    
        return 0;
    }
    
    void function(int *a) {
        printf("Pointer memory address is %p\n", (void*)a);
        printf("The pointer value is %d\n", *a);
    }
    Observe that the function named function is called three times with different arguments.

    The function(&value) call passes &value as an argument because value is an int, so &value is a pointer to int. The formal parameter named a is a pointer to int, so this matches exactly.

    The function(pValue) call passes pValue as an argument because pValue is a pointer to int. The formal parameter named a is a pointer to int, so this matches exactly.

    The function(*ppValue) call passes *ppValue as an argument because ppValue is a pointer to a pointer to int, so *ppValue is a pointer to int. The formal parameter named a is a pointer to int, so this matches exactly.

    Also, notice that I changed this:
    Code:
    printf("Pointer memory address is %p\n", a);
    to this:
    Code:
    printf("Pointer memory address is %p\n", (void*)a);
    The reason is that the %p format specifier expects a corresponding pointer to void argument, so I cast a to void* to match.

    EDIT:
    By the way, the text for the printf statements are not correct. I would have written:
    Code:
    printf("The pointer value is %p\n", (void*)a);
    printf("The value of what the pointer points to is %d\n", *a);
    The reason is that the value of a pointer is an address, i.e., the address of what the pointer points to. The address of the pointer itself is something else, in this case it would have been &a.
    Last edited by laserlight; 02-25-2016 at 02:11 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi all..

    One thing I would suggest from my limited understanding is...not to think of the (*) as being a pointer. But look at how its presented....the * character is also a dereferencing character for pointers, so look at the format as a whole.

    (int * pValue) to me is a variable(pValue) with type ( int*) ie Pointer, just like double, int, char.

    And once a variable is declared, to give it a value you don't have to use its type anymore, you decladed it already, jst use its name.
    So in your code, would be (pValue = &value) in this case the variable value is a memory address.

    As your function is asking for a pointer ( as shown by the type ( int*) you pass pValue to it, as it contains the memory address of the (int) of interest.

    Just thought I would try explain also....as much for myself as for Steve.

    John.

  4. #4
    Registered User
    Join Date
    Oct 2015
    Posts
    4
    Quote Originally Posted by laserlight View Post
    It might be a little more instructive to try this:
    Code:
    #include <stdio.h>
    
    void function(int *a);
    
    int main(void) {
        int value = 6;
        int *pValue = &value;
        int **ppValue = &pValue;
        function(&value);
        function(pValue);
        function(*ppValue);
    
        return 0;
    }
    
    void function(int *a) {
        printf("Pointer memory address is %p\n", (void*)a);
        printf("The pointer value is %d\n", *a);
    }
    Observe that the function named function is called three times with different arguments.

    The function(&value) call passes &value as an argument because value is an int, so &value is a pointer to int. The formal parameter named a is a pointer to int, so this matches exactly.

    The function(pValue) call passes pValue as an argument because pValue is a pointer to int. The formal parameter named a is a pointer to int, so this matches exactly.

    The function(*ppValue) call passes *ppValue as an argument because ppValue is a pointer to a pointer to int, so *ppValue is a pointer to int. The formal parameter named a is a pointer to int, so this matches exactly.

    Also, notice that I changed this:
    Code:
    printf("Pointer memory address is %p\n", a);
    to this:
    Code:
    printf("Pointer memory address is %p\n", (void*)a);
    The reason is that the %p format specifier expects a corresponding pointer to void argument, so I cast a to void* to match.

    EDIT:
    By the way, the text for the printf statements are not correct. I would have written:
    Code:
    printf("The pointer value is %p\n", (void*)a);
    printf("The value of what the pointer points to is %d\n", *a);
    The reason is that the value of a pointer is an address, i.e., the address of what the pointer points to. The address of the pointer itself is something else, in this case it would have been &a.

    Thanks for this.. Something I picked up on was the = function(&value) ..
    Obviously pointers are important, however why are they needed if instead we could just write the code &variable instead ?

    Thanks !

  5. #5
    Registered User
    Join Date
    Oct 2015
    Posts
    4
    Quote Originally Posted by JohnGM View Post
    Hi all..

    (int * pValue) to me is a variable(pValue) with type ( int*) ie Pointer, just like double, int, char.

    And once a variable is declared, to give it a value you don't have to use its type anymore, you decladed it already, jst use its name.
    So in your code, would be (pValue = &value) in this case the variable value is a memory address.
    Very good thanks John, That does actually make sense !
    Last edited by Steve Clifton; 02-25-2016 at 07:00 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,863
    Quote Originally Posted by Steve Clifton
    Obviously pointers are important, however why are they needed if instead we could just write the code &variable instead ?
    Well, if you use &variable, you get a pointer to variable, so...
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    68
    When to use * in front of the pointer name, think if you want to know the value underneath that pointer or is it the address? (no * and it's the address)
    When you pass a pointer to a function that wants a pointer as its input, you of course don't want to pass the value underneath the address do you.

    As it the compiled code will look something like this:

    mov #0x1020,R12 // with no * ( could be the flash address of const string location etc)
    call #function

    mov #0x1020,R12
    mov.b (R12), R12 // with the *, use indirect addressing to get underlying byte value.
    call #function

    You use & as way of casting the var's address to a pointer, most compilers will not even allow you set a declared pointer to an int value if you forget the &

    But sometimes when you have absolute memory locations stored somewhere, then you have to cast: mypnt = (char*) 0x1000;
    Last edited by tonyp12; 02-26-2016 at 02:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Muting from functions to pointers to functions
    By Fiskker in forum C Programming
    Replies: 2
    Last Post: 06-18-2015, 12:57 PM
  2. Help with FUNCTIONS WITH POINTERS
    By Sankait Laroiya in forum C Programming
    Replies: 7
    Last Post: 09-25-2014, 04:51 AM
  3. HELP WITH FUNCTIONS and POINTERS!!!
    By cjohnson412 in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2008, 10:48 PM
  4. Replies: 1
    Last Post: 01-20-2002, 11:50 AM