0

I know, the title is a little bit odd. Of course I know how to do call-by-reference in C. My problem is simple and the solution could be simple as well. I am currently going through some scripts of my university and they explicitly write "Call-By-Reference is NOT used in C".

Since I (think I) know how to do call by reference in C, i am a little bit confused and hope you can help me out.

Do I generally understand something wrong about the term Call-By-Reference? Or is the code below actually call-by reference

void test(int* p) {
    *p = 5;
}

int main(int argc, char *argv[]) {
    int i = 2;
    test(&i);
    printf("i = %i\n",i);
    end;
}

Another explanation would be that call-by-reference is bad practice. For example Java does not allow call by reference for non objects. But since C is nor object oriented it makes no sense to me (beside maybe with structs)

Or is the script actually wrong?

Thank you in advance!

4 Answers4

1

There is no concept of variable reference in C, as there is in C++. What you show in your small example is simply a use of a pointer (or variable address). But you are passing a value (the address of the variable) not a reference (an alias of the variable name).

Guillaume Petitjean
  • 2,408
  • 1
  • 21
  • 47
0

Or is the script actually wrong?

There is nothing wrong in your code.

This call test(&i); actually is pass by pointer (not by reference). In C, there is no concept about reference. The & here is address-of operator, not a reference.

The concept pass-by-reference is only applicable in C++, which means to pass the object itself (instead of passing a copy of it). Even that, though, the call to test would be different: test(i); // pass-by-reference instead of test(&i); // pass-by-pointer.

artm
  • 17,291
  • 6
  • 38
  • 54
0

"Call-By-Reference is NOT used in C"

This is a totally wrong statement.

Each programming language has its own terminology. And in C call by reference or pass by reference means pass indirectly through a pointer to an object.

This wrong statement arises when somebody is saying about C but is using the terminology of the definition of the term reference introduced in other languages.

From the C Standard (6.2.5 Types)

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.

So pass by reference in C means the above context when a referenced type is used to deal with a referenced object. It is not a normative term however it is used in C to specify this approach of passing objects to functions through pointers.

In your program

#include <stdio.h>

void test(int* p) {
    *p = 5;
}

int main(int argc, char *argv[]) {
    int i = 2;
    test(&i);
    printf("i = %i\n",i);
    end;
}

you are passing the object i by reference through the pointer expression &i. So this expression *p = 5; changes the referenced object that is the referenced object is passed by reference in the terminology of C.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • "This is a totally wrong statement". We don't know the context of this quote. I assume the sentence is a comparison with C++ where there is a difference between pointer and reference, that does not exist in C – Guillaume Petitjean Nov 26 '19 at 10:55
  • @GuillaumePetitjean You could say for example that in C there is no such entities as references as they are defined in C++ with their special meaning. But this a totally different phrase. – Vlad from Moscow Nov 26 '19 at 11:10
  • @GuillaumePetitjean The phrase in ant case is totally wrong without any context. – Vlad from Moscow Nov 26 '19 at 11:13
-1

Reference is a concept that is used in C++ but not in C. In C we ususally use pointers and designate it as an address when it is about manipulating a struct "by reference" in stead of using a "by value" argument passing mechanism. Your code is just fine. The problem with pointers usually happens in C when you start allocating memory for a variable. The question which arises afterward is what code is responsible to free the allocated memory block. By the way anytime you write a function using a ponter you should test if it is not null

    void test(int* p) {
        if(p != NULL) {
            *p = 5;
        }
    }

    int main(int argc, char *argv[]) {
        int i = 2;
        test(&i);
        printf("i = %i\n",i);
        // end; // this is not a C instruction
        exit(0);
    }
pascal sautot
  • 375
  • 6
  • 20