Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Programming - Pass-by-Reference

In the C program below, I don't understand why buf[0] = 'A' after I call foo. Isn't foo doing pass-by-value?

#include <stdio.h>
#include <stdlib.h>

    void foo(char buf[])
    {
      buf[0] = 'A';
    }

    int main(int argc, char *argv[])
    {
      char buf[10];

      buf[0] = 'B';
      printf("before foo | buf[0] = %c\n", buf[0]);
      foo(buf);
      printf("after foo | buf[0] = %c\n", buf[0]);

      system("PAUSE"); 
      return 0;
      }

output:

before foo | buf[0] = 'B' 
after foo | buf[0] = 'A'
like image 465
Kevin Meredith Avatar asked Aug 05 '10 13:08

Kevin Meredith


People also ask

What is pass-by-reference in C program?

Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function. In C, the corresponding parameter in the called function must be declared as a pointer type.

Does C use pass-by-reference?

The correct statement is "C does not support implicitly passing a variable by reference" -- you need to explicitly create a reference (with & ) before calling the function and explicitly dereference it (with * ) in the function.

What is pass by value and pass-by-reference in C?

"Passing by value" means that you pass the actual value of the variable into the function. So, in your example, it would pass the value 9. "Passing by reference" means that you pass the variable itself into the function (not just the value). So, in your example, it would pass an integer object with the value of 9.

Which programming language is pass-by-reference?

Pass by reference is something that C++ developers use to allow a function to modify a variable without having to create a copy of it. To pass a variable by reference, we have to declare function parameters as references and not normal variables.


3 Answers

void foo(char buf[])

is the same as

void foo(char* buf)

When you call it, foo(buf), you pass a pointer by value, so a copy of the pointer is made.

The copy of the pointer points to the same object as the original pointer (or, in this case, to the initial element of the array).

C does not have pass by reference semantics in the sense that C++ has pass by reference semantics. Everything in C is passed by value. Pointers are used to get pass by reference semantics.

like image 129
James McNellis Avatar answered Oct 20 '22 19:10

James McNellis


an array is just a fancy way to use a pointer. When you pass buf to the function, you're passing a pointer by value, but when you dereference the pointer, you're still referencing the string it points to.

like image 21
Nathan Fellman Avatar answered Oct 20 '22 18:10

Nathan Fellman


Array as function parameter is equivalent to a pointer, so the declaration

void foo( char buf[] );

is the same as

void foo( char* buf );

The array argument is then decayed to the pointer to its first element.

like image 22
Nikolai Fetissov Avatar answered Oct 20 '22 19:10

Nikolai Fetissov