pointers in Monkey C - bug or by design?

this code

function f(arr)
    {
        arr[0]=11;
        arr[1]=12;
        arr[2]=13;
    }

    function test()
    {
        var x = [1,2,3];
        SYS.println(x);
        f(x);
        SYS.println(x);

    }

produces in console:
[1, 2, 3]
[11, 12, 13]

It means that f  modified external var x. Is it by design or bug?

sdk 4.1b, 4.0.6, 4.0.5...
eclipse CIQ plug in: 4.1.0.beta1
eclipse ver: 2021-09 (4.21.0) Build id: 20210910-1417
windows 10
minSdkVersion 2.4.0
java jre1.8.0_311

  • No, that link is showing arrays passed by reference.

    "Because arrays are reference types, the method can change the value of the elements."

    Also, this stackoverflow post discusses the same thing (C# and passing arrays to functions):

    https://stackoverflow.com/questions/10325323/passing-arrays-by-value-and-by-reference

    Considering the code in your question, an array is a reference type and so for this function:

    public static void FirstDouble(int[] array)
    

    the variable array is actually a reference, because int[] is a reference type. So array is a reference that is passed by value.

    At the very least we agree that not everyone means what *you* mean when they say pass by value or pass by reference. We could say that they're all wrong (but I wouldn't). I would say that the meaning of "pass by value" is really context-dependent.

  • I'm just trying to provide some context behind why "pass by value" is an overloading and confusing term.

    In C, the value being passed for arrays is the memory address of the first element.

  • In C, the value being passed for arrays is the memory address of the first element.

    Thanks, I realize that in C, "the name of an array is an alias for a pointer to its first element." Which causes problems when noobs try to evaluate sizeof(array) in a compilation unit other than where array is defined (because they get 4 instead of the array size.)

    What's your point?

    I was trying to clarify to the OP and anyone else who cares to read this discussion why the OP might have gotten confused when they were told Monkey C is "pass by value."

    Obviously they interpreted "pass by value" one way, when it was meant a different way.

    To further my point, you believe that arrays are "passed by reference in C#, and you claim that the statement "arrays are passed by value in C#" is *incorrect*, when various sources (including Microsoft itself) either imply or outright state that *array references are passed by value* in C#.

    So obviously not everyone means the same thing when they say "pass by value/reference". (Especially depending on the context) And that's all this is - a discussion about semantics. None of it changes any of the actual behavior of any of these languages.

  • Thanks, I realize that in C, "the name of an array is an alias for a pointer to its first element." Which causes problems when noobs try to evaluate sizeof(array) in a compilation unit other than where array is defined (because they get 4 instead of the array size.)

    What's your point?

    I'm talking about what a function sees with an array passed as an argument to a function in C.

    The name is "the name of an array is an alias for a pointer to its first element." only in the scope of the array.

    int array[10];

    *array = 1; // alias for first element.

    f(array);

    f(array+3);

    ==============

    f(int* array)

    {

    // The name here is associated with some value on the stack (separate from the array in the calling block).

    // It most-assuredly is NOT an alias to the first element.

    array +=2; // array is a copy of the value of the memory address of the first element. This doesn't affect the array in the calling block.

    }

  • I'm talking about what a function sees with an array passed as an argument to a function in C.

    The name is "the name of an array is an alias for a pointer to its first element." only in the scope of the array.

    Thanks I understand all of that and I understand how arrays and pointers work in C.

    Is there something specific where I accidentally posted misinformation???

    Wow.

  • Like I'm sorry I said "the name of an array is an alias to a pointer to its first element".

    Next time I'll be sure to use the exact wording from the C specification.

    Meanwhile you posted stuff that directly contradicts what Microsoft says about C# and haven't owned up to it but ¯\_(ツ)_/¯

    I don't even know what we're arguing about because there's no substantive difference between anything that either of us is saying. It's just arguments about the right words to use.

  • ???

    The person asking the question might not be clear on it.

    You also asked "what was the point?"

    The point was passing arguments to functions. The "alias" stuff has nothing to do with that.

  • Meanwhile you posted stuff that directly contradicts what Microsoft says about C# and haven't owned up to it but

    I provide a quote from "what Microsoft says".

    "Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements."

    It contradicts itself. 

  • You brought this up:

    "In C, the value being passed for arrays is the memory address of the first element."

    I was agreeing with you.

    It actually does because in most contexts when you have:

    int x[] = { 1, 2, 3, 4, 5,6  }; // for example

    x is actually the exact same thing as &x[0];

    that's what I meant.

    But in *one specific case*, it's not true.

    sizeof(x), in the same compilation unit as x, is actually the size of the array and not the size of the pointer.

  • It doesn't contradict itself if you read this:

    docs.microsoft.com/.../ref

    Don't confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. A method parameter can be modified by ref regardless of whether it is a value type or a reference type. There is no boxing of a value type when it is passed by reference.