1

I recently asked this question: Why aren't my pointers to integer arrays working?

One of the suggestions was to use regular variables instead of pointers pointing to variables.

I've programmed in Java in the past. If I understand correctly, if you have this code in Java:

void test(b) {
    b++;
}

void main() {
    int a = 0;
    test(a);
}

Then a would remain at 0. But if I understood correctly, in C, if I have this code:

void test(b) {
    b++;
}

int main() {
    int a = 0;
    test(a);
    return 0;
}

Then a becomes 1?

Community
  • 1
  • 1
user1251385
  • 197
  • 1
  • 1
  • 8

4 Answers4

4

No , in c passing a value makes a copy of that value, and the original variable isn't changed.

Now, you can pass a value of the address of that variable, then in the function you look where the passed address is pointing at, and change the value at that address and voila the original variable is changed.

2

No. In the C example, the a would still retain its original value. In C, all parameters are passed by value, while in Java, only primitive types are passed by value, all others are passed by reference. To achieve the effect you describe, your code should look like this:

void test(int* b) {
    (*b)++;
}

int main() {
   int a = 0;
   test(&a);
   return 0;
}

Here, test accepts a pointer to int which it then dereferences (gets the value that lies in the memory on the specified address) and increases it by 1. In main() you pass a address of a.

Andrej Palicka
  • 971
  • 1
  • 11
  • 26
  • In Java, "all others" are passed by value as well, just value of the reference. – jedwards Apr 01 '13 at 18:03
  • 2
    Java is just like C in that there is no pass-by-reference: all types, both primitives and references/pointers, are passed *by value*. The difference in terminology (pass-by-reference vs pass-by-value) is important because there are languages that do have pass-by-reference. – Joni Apr 01 '13 at 18:07
  • Hmmm, I've been living in a lie my whole life :-) I thought that what Java does with its objects is in fact pass by reference. What does pass-by-reference means then? – Andrej Palicka Apr 01 '13 at 18:14
  • @akcilap You can think of it as pass-by-value, but Java only ever gives you pointers to objects. – Nicholas Wilson Apr 18 '13 at 09:52
1

Wait a moment."In C, arguments are passed to functions by value while other languages may pass variables by reference. This means that the receiving function gets copies of the values and has no direct way of altering the original variables. For a function to alter a variable passed from another function, the caller must pass its address (a pointer to it), which can then be dereferenced in the receiving function. See Pointers for more information."

joan
  • 2,407
  • 4
  • 29
  • 35
0

In both languages, the passing of the variable is by copying in the value. It's just that with C, you can pass a memory location (pointer) as the value, and effectively get pass by reference.

In Java, you could do the same thing (without actually having a memory address value) by passing in a reference; however, java built-in types cannot be passed by reference as only objects can be referred to by reference. Note that arrays are actually objects in java, so some of the rules must be interpreted as objects when dealing with arrays, even if they are arrays of Java primitives.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138