简体   繁体   中英

C - Pass By Value and Pass By Reference Inconsistency

When using pass by reference in C, sometimes the address passed to the called function changes:

void mainFunction()
{
  BYTE aAddress[10];
  readFunction(aAddress); // Address of aAddress: 0x111111
}

void readFunction(BYTE *pAddress) // Address of pAddress: 0x222222
{
  ...
}

Another issue I encountered, when using pass by value, the value received by the called function differs from what is passed to it.

void mainFunction()
{
  readFunction(0x20); // Passed value: 0x20
}

void readFunction(BYTE uValue) // Received value: 0x00
{
  ...
}

It all depends: How did you print the addresses? If you get the address of the variable, then of course they will be different. The value the pointers point to, however, will be the same::

int main ( void )
{
    char some_array[100];
    printf("array holds: %p\n", (void*) some_array);//same
    printf("Address in main: %p\n", (void*) &some_array[0]);//same
    pass_arr(some_array);
    return 0;
}
void pass_arr(char *arr)
{
    printf("Address of pointer VAR: %p\n", (void *) &arr);//PRINTS DIFFERENT ADDRESS
    printf("Pointer address: %p\n", (void *) arr);//same
    printf("points to: %p\n", (void *) &(*arr));//same
}

check this codepad
You probably failed to print the address of a variable correctly, the only right way is to use the %p placeholder, and to cast the address to void * . This is down to the fact that the way addresses are printed out is implementation dependent.


That said, you mention "pass by reference" . C doesn't pass by reference, it doesn't have references in the C++ sense. But in order for a function to alter the value of something else, you ought to pass a pointer to a pointer:

struct some_str
{
    int mem1;
    size_t mem2;
};
int main ( void )
{
    struct some_str *foo = malloc(sizeof *foo);
    set_value(&foo);
    free(foo);
    return 0;
}
void set_value(struct some_str **change)
{//pointer to pointer
    static int change_count = 0;
    (*change)->mem1 = 123;
    (*change)->mem2 = ++change_count;//for example
}

C always passes by value, in case of a pointer, that value is a memory address, in case of a primitive type ( int , char , long ...), it's not the variable but the value you're passing, if you pass a pointer to the variable ( &my_int ), you're not passing the variable, but its address in memory. Those are the only 2 options you have to choose from.

For more on the subject (arrays vs pointers, and why arrays decay into pointers in most cases you may find this link useful )

When using pass by reference in C, sometimes the address passed to the called function changes

--> NO. Look at this code

#include <stdio.h>

void readFunction (int *pAddress) 
{
    printf("Address of pAddress: %p\n", &pAddress);
    printf("Content of pAddress: %p\n", pAddress);
}

void main()
{
    int a[10];
    printf("Address of a: %p\n", &a);
    readFunction(a);
}

when execute it, you will get something as:

Address of a: 0xbfb94948
Address of pAddress: 0xbfb94930
Content of pAddress: 0xbfb94948

The pAddress is located in another place but it still points to the address of a . And this is the primitive of pass by reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM