Direct invocation vs indirect invocation in C

Posted by Mohit Deshpande on Stack Overflow See other posts from Stack Overflow or by Mohit Deshpande
Published on 2010-04-04T17:42:37Z Indexed on 2010/04/04 17:53 UTC
Read the original article Hit count: 403

I am new to C and I was reading about how pointers "point" to the address of another variable. So I have tried indirect invocation and direct invocation and received the same results (as any C/C++ developer could have predicted). This is what I did:

int cost;
int *cost_ptr;

int main()
{
    cost_ptr = &cost;                          //assign pointer to cost
    cost = 100;                                //intialize cost with a value
    printf("\nDirect Access: %d", cost);
    cost = 0;                                  //reset the value
    *cost_ptr = 100;
    printf("\nIndirect Access: %d", *cost_ptr);
    //some code here

    return 0;                                  //1
}

So I am wondering if indirect invocation with pointers has any advantages over direct invocation or vice-versa. Some advantages/disadvantages could include speed, amount of memory consumed performing the operation (most likely the same but I just wanted to put that out there), safeness (like dangling pointers) , good programming practice, etc.
1Funny thing, I am using the GNU C Compiler (gcc) and it still compiles without the return statement and everything is as expected. Maybe because the C++ compiler will automatically insert the return statement if you forget.

© Stack Overflow or respective owner

Related posts about pointers

Related posts about invocation