编译器如何理解给定函数是通过值传递还是通过引用传递?

时间:2014-08-20 07:05:13

标签: c compiler-construction reference arguments

C编译器如何理解给定函数是通过值传递还是通过引用传递? 如果变量的指针作为整数(按值传递)传递给函数会发生什么?或者这可能在C?

ie:将变量的地址复制到另一个变量(int),并将该变量传递给函数。这里被调用的函数将获得一个正常整数参数的地址。这可能在C?如果不是为什么?

2 个答案:

答案 0 :(得分:1)

在C中,只有传递值(通过引用仅在C ++中支持)。

标准变量和指针变量都按值传递。因此,编译器不得对引用或值进行任何检查。

标准变量和指针变量都相同,都是存储值,但是指针变量只能存储地址值并支持解除引用运算符'*',此运算符获取指针变量指向的值。

由于这个,当你传递值(C只支持值)一个指针变量时,指针变量的值被复制到一个本地指针变量中(在函数堆栈中实例化)这个变量是指针参数功能。因此,您可以使用解除引用运算符访问本地指针变量中复制的地址值。

已添加评论08/22/14

我可以用一个例子来解释:

void FuncSet10(int* PtrArgument)
{
    // The PtrArgument is a local pointer variable
    // it is allocated in the stack. The compiler
    // copies the MyIntVariable address in this variable

    // Using dereferencing operator I can access to
    // memory location pointed by PtrArgument
    *PtrArgument = 10;


}

void FuncSet20(int IntArgument)
{
    // The IntArgument is a local variable
    // allocated in the stack. The compiler
    // copies the MyIntVariable address in this variable

    // this code emulates the dereferencing operator
    // and so poiter mechanism.
    *((int*)IntArgument) = 20;
}

void FuncMovePointer(int* pointer)
{
    int a = *pointer++;  // now a contains 1
    int b = *pointer++;  // now b contains 2
    int c = *pointer++;  // now c contains 3

    printf("a contains %d\n", a);
    printf("b contains %d\n", b);
    printf("c contains %d\n", c);

    // The *Ptr now points to fourth (value 4) element of the Array
    printf("pointer points to %d\n", *pointer);
}


int _tmain(int argc, _TCHAR* argv[])
{

    int Array[] = {1, 2, 3, 4}; // Array definition
    int* pointer = Array;       // pointer points to the array

    int MyIntVariable;

    // First example for explanation of
    // the pointer mechanism and compiler actions

    // After calling this function MyIntVariable contains 10
    FuncSet10(&MyIntVariable);

    printf("MyIntVariable contains %d\n", MyIntVariable);

    // After calling this function  MyIntVariable contains 20
    // This code emulate pointer mechanism
    FuncSet20((int)&MyIntVariable);

    printf("MyIntVariable contains %d\n", MyIntVariable);

    // Second example to demonstrate that a pointer
    // is only a local copy in a called function

    // Inside function the pointer is incremented with ++ operator
    // so it will point the next element before returning by function
    // (it should be 4)
    FuncMovePointer(pointer);

    // But this is not true, it points always first element! Why?
    // Because the FuncMovePointer manages a its local copy!

    printf("but externally the FuncMovePointer it still points to first element %d\n", *pointer);

    return 0;
}

我希望上面的代码可以帮助您更好地理解。

安吉洛

答案 1 :(得分:0)

  

C编译器如何理解给定函数是否执行a   按值传递或通过引用传递?

有关通过引用传递的信息C无法理解。您可以发送指向变量的指针。

  

如果变量的指针作为整数传递会发生什么(如   通过值传递给函数?

将指针传递给变量并使用函数内的解引用来访问它。通过将指针传递给函数,您可以允许该函数读取和写入存储在该变量中的数据。

例如

void myfunc( int *myval ) // pointer "myval" point address of "locvar"
{
   *myval = 77; //place value 77 at address pointed by "myval"
}
int main()
{
    int locvar=10; //place value 10 at some address of "locvar"
    printf("locvar = %d\n",locvar);
    myfunc(&locvar);    // Here address of "locvar" passed 
    printf("locvar = %d\n",locvar);
    return 0;
}