Are pointers considered a method of calling by reference in C?

后端 未结 8 1626
暖寄归人
暖寄归人 2021-01-30 10:31

In my University\'s C programming class, the professor and subsequent book written by her uses the term call or pass by reference when referring to poin

相关标签:
8条回答
  • 2021-01-30 10:39

    Does C even have ``pass by reference''?

    Not really.

    Strictly speaking, C always uses pass by value. You can simulate pass by reference yourself, by defining functions which accept pointers and then using the & operator when calling, and the compiler will essentially simulate it for you when you pass an array to a function (by passing a pointer instead, see question 6.4 et al.).

    Another way of looking at it is that if an parameter has type, say, int * then an integer is being passed by reference and a pointer to an integer is being passed by value.

    Fundamentally, C has nothing truly equivalent to formal pass by reference or c++ reference parameters.

    To demonstrate that pointers are passed by value, let's consider an example of number swapping using pointers.

    int main(void)
    {
        int num1 = 5;
        int num2 = 10;
    
        int *pnum1 = &num1;
        int *pnum2 = &num2;
        int ptemp;
    
        printf("Before swap, *Pnum1 = %d and *pnum2 = %d\n", *pnum1, *pnum2);
        temp = pnum1;
        pnum1 = pnum2;
        pnum2 = ptemp;
    
        printf("After swap, *Pnum1 = %d and *pnum2 = %d\n", *pnum1, *pnum2);
    }
    

    Instead of swapping numbers pointers are swapped. Now make a function for the same

    void swap(int *pnum1, int *pnum2)
    {
         int *ptemp = pnum1;
         pnum1 = pnum2;
         pnum2 = temp;
    }
    
    int main(void)
    {
        int num1 = 5;
        int num2 = 10;
    
        int *pnum1 = &num1;
        int *pnum2 = &num2;
    
        printf("Before swap, *pnum1 = %d and *pnum2 = %d\n", *pnum1, *pnum2);
        swap(pnum1, pnum2);
    
        printf("After swap, *pnum1 = %d and *pnum2 = %d\n", *pnum1, *pnum2);
    }
    

    Boom! No swapping!


    Some tutorials mention pointer reference as call by reference which is misleading. See the this answer for the difference between passing by reference and passing by value.

    0 讨论(0)
  • 2021-01-30 10:49

    In languages which support pass-by-reference, there exists a means by which a function can be given something that can be used to identify a variable know to the caller until the called function returns, but which can only be stored in places that won't exist after that. Consequently, the caller can know that anything that will be done with a variable as a result of passing some function a reference to it will have been done by the time the function returns.

    Compare the C and C# programs:

    // C               // C#
    int x=0;           int x=0;
    foo(&x);           foo(ref x);
    x++;               x++;
    bar();             bar();
    x++;               x++;
    boz(x);            boz(x);
    

    The C compiler has no way of knowing whether "bar" might change x, because foo() received an unrestricted pointer to it. By contrast, the C# compiler knows that bar() can't possibly change x, since foo() only receives a temporary reference (called a "byref" in .NET terminology) to it and there is no way for any copy of that byref to survive past the point where foo() returns.

    Passing pointers to things allows code to do the same things that can be done with pass-by-ref semantics, but pass-by-ref semantics make it possible for code to offer stronger guarantees about things it won't do.

    0 讨论(0)
  • 2021-01-30 10:50

    From the C99 standard (emphasis mine):

    6.2.5 Types

    20 Any number of derived types can be constructed from the object and function types, as follows:

    ...

    — A pointer type may be derived from a function type or an object type, called the referenced type. A pointer type describes an object whose value provides a reference to an entity of the referenced type. A pointer type derived from the referenced type T is sometimes called ‘‘pointer to T’’. The construction of a pointer type from a referenced type is called ‘‘pointer type derivation’’. A pointer type is a complete object type.

    Based on the above, what your professor said makes sense and is correct. A pointer is passed by value to functions. If the pointer points to a valid entity, its value provides a reference to an entity.

    0 讨论(0)
  • 2021-01-30 10:51

    C passes arguments by value, period. However, pointers are a mechanism that can be used for effectively passing arguments by reference. Just like a coin can be used effectively as a screw driver if you got the right kind of screw: some screws slit are even chosen to operate well with coins. They still don't turn the coins into actual screw drivers.

    C++ still passes arguments by value. C++ references are quite more limited than pointers (though having more implicit conversions) and cannot become part of data structures, and their use looks a lot more like the usual call-by-reference code would look, but their semantics, while very much catered to match the needs of call-by-reference parameters, are still more tangible than that of pure call-by-reference implementations like Fortran parameters or Pascal var parameters and you can use references perfectly well outside of function call contexts.

    0 讨论(0)
  • 2021-01-30 10:55

    Your professor is right. By value , it is copied. By reference, it is not copied, the reference says where it is.

    By value , you pass an int to a function , it is copied , changes to the copy does not affect the original.

    By reference , pass same int as pointer , it is not copied , you are modifying the original.

    By reference , an array is always by reference , you could have one billion items in your array , it is faster to just say where it is , you are modifying the original.

    0 讨论(0)
  • 2021-01-30 10:58

    Reference is an overloaded term here; in general, a reference is simply a way to refer to something. A pointer refers to the object pointed to, and passing (by value) a pointer to an object is the standard way to pass by reference in C.

    C++ introduced reference types as a better way to express references, and introduces an ambiguity into technical English, since we may now use the term "pass by reference" to refer to using reference types to pass an object by reference.

    In a C++ context, the former use is, IMO, deprecated. However, I believe the former use is common in other contexts (e.g. pure C) where there is no ambiguity.

    0 讨论(0)
提交回复
热议问题