Thread: Function Call

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Function Call

    function prototype

    void test (const struct addr*);


    calling the function
    test( (struct addr *) &something);

    The prototype is expecting a parameter of pointer to the structure addr,
    but the calling function is supplying the address of the pointer of the structure addr.

    is this correct?
    or test((struct addr*) something) is the right way?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Is 'something' a pointer to struct or a struct?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    something maybe is an address of a pointer to the struct.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by lilzz View Post
    something maybe is an address of a pointer to the struct.
    So if you don't really know what something is, you can't properly pass it around. If something is an address of a pointer to the struct, here is how I would expect it to be declared/used:
    Code:
    struct addr bar;  // a plain old struct
    struct addr *foo = &bar;  // foo is a pointer to a struct
    struct addr **something = &foo;  // something contains the address of a pointer to a struct
    ...
    test(*something);  // call test with a pointer to a struct (dereferencing the double pointer to a single pointer)
    test(foo);  // no need to do anything, foo is the proper type
    test(&bar);  // bar is a struct, we need the address of (a pointer to) the struct

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Quote Originally Posted by anduril462 View Post
    Code:
    struct addr bar;  // a plain old struct
    struct addr *foo = &bar;  // foo is a pointer to a struct
    struct addr **something = &foo;  // something contains the address of a pointer to a struct
    ...
    test(*something);  // call test with a pointer to a struct (dereferencing the double pointer to a single pointer)
    test(foo);  // no need to do anything, foo is the proper type
    test(&bar);  // bar is a struct, we need the address of (a pointer to) the struct
    is there a case for
    test(&foo); ?
    because I have seen int * x; some_function(&x) used in a program.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by lilzz View Post
    is there a case for
    test(&foo); ?
    because I have seen int * x; some_function(&x) used in a program.
    Yes, if you want to change the address that x points to from within some_function, but have that change be visible outside some_function. E.g.:
    Code:
    void alloc_something(struct something **x)
    {
        *x = malloc(sizeof(**x));  // allocate a struct something, store it's address in *x
    }
    
    struct something *other_alloc_something(void)
    {
        return malloc(sizeof(struct something));  // allocate a struct something, return it's address
    }
    
    int main(void)
    {
        struct something *x, *y;
    
        alloc_something(&x);  // allocates a struct something and puts the address in x
        y = other_alloc_something();  // also allocates a struct something and puts the address in y
    
        free(x);
        free(y);
    }
    EDIT: I prefer the other_alloc_something style, but either way you get the same results. In the first method, you have to check if x is NULL before you can safely do *x.
    Last edited by anduril462; 02-23-2011 at 04:47 PM.

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by lilzz View Post
    is there a case for
    test(&foo); ?
    because I have seen int * x; some_function(&x) used in a program.
    Yes, there is. Calling a function with a set of arguments creates COPIES of the arguments that the function then manipulates. Then, when the function ends, the copies are destroyed. So if you wanted to write a program that swaps two numbers and you wrote

    Code:
    void swap( int a, int b ) {
       int temp;
       temp = a;
       a = b;
       b = temp;
       return;
    }
    you would find that nothing happened, because the function swapped COPIES of a and b but left the original a and b untouched. What you would need to do is pass the function the addresses of a and b so that the function can modify the information at those memory locations; those changes will remain in place when you return from the function. Behold:

    Code:
    void swap( int *a, int *b ) {
       int temp;
       temp = *a;
       *a = *b;
       *b = temp;
       return;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM