cpointerspass-by-referencepass-by-valuecall-by-value

How can I understand the concept of pointers (*) and address-of (&) operators?


I am trying to understand the significance of these two operators, so I wrote this code just for that purpose.

#include <stdio.h>
#include <string.h>

int main()
{
    char *mnemonic, *operands;

    mnemonic = "add";
    operands = "five to two";

    analyse_inst(mnemonic, operands);

}

void analyse_inst(char mnemonic, char operands)
{
    printf("%s", mnemonic);
    printf("%s", operands);
}

However, I noticed that it wouldn't work unless I change the arguments of analyse_inst() function to analyse_inst(char * mnemonic, char * operands), which means that I will be passing pointers to the function. But why is that required?

Also, I looked up about "passing by reference." And according to tutorialspoint.com, its definition:

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

From that, I got that passing a variable by reference and then modifying that value would mean that the same variable outside the function would be changed as well; whereas for passing a variable by value would not change the same variable located outside the function.

Am I going wrong anywhere?

How can I can modify my code such that I am passing the two variables by reference?

(P.S. I have read other Stack Overflow threads on the same topic, but I would appreciate it if anyone could explain it in the context of the code I wrote)


Solution

  • which means that I will be passing pointers to the function. But why is that required?

    Because what you have in main are pointers, and what printf("%s" expects is a char*.

    "Pass by reference" is a broad term in programming, meaning passing along an address rather than a copy of the object. In your case, you pass a pointer to the first element of each string, rather than making a copy of the whole string, since that would waste execution time and memory.

    So while the strings themselves could be said to be "passed by reference", strictly speaking C actually only allows parameters to be passed by value. The pointers themselves are passed by value. Your function parameters will be copies of the pointers you have allocated in main(). But they point at the same strings as the pointers in main() do.

    From that, I got that passing a variable by reference and then modifying that value would mean that the same variable outside the function would be changed as well;

    Indeed, you could change the string from inside the function through the pointer and then it would affect the string in main(). But in this case, you haven't allocated any memory to modify - you would attempt to modify a string literal "...", which would have been a bug. If you were to modify the strings, you should have declared them as arrays in main(): char mnemonic[] = "add";

    Now as it turns out, whenever you use an array like the one in my example inside an expression, it "decays" into a pointer to the first element. So we wouldn't actually be able to pass the array by value to the function, as the C language would have changed it between the lines to a pointer to the first element.

    You can play around with this code:

    #include <stdio.h>
    #include <string.h>
    
    void analyse_inst(char* mnemonic, char* operands);
    
    int main()
    {
        char mnemonic[] = "add";
        char operands[] = "five to two";
    
        analyse_inst(mnemonic, operands);
        printf("%s\n", mnemonic);
    }
    
    void analyse_inst(char* mnemonic, char* operands)
    {
        printf("%s ", mnemonic);
        printf("%s\n", operands);
    
        strcpy(mnemonic, "hi");
    }