0

I've got a problem. I have a function (It doesn't return any information) and array. I need to write information into array inside of function that is inside of another function.

It should be look like this example, but in javascript:

#include <stdio.h>

void setDefault(int * array);
void setDefault2(int * array2);


int main(void)
{
    int a[]={0,1,2};
    setDefault(a);
    printf("%d\n",a[0]);
    return 0;
}

void setDefault(int * array)
{
    setDefault2(array);
}

void setDefault2(int * array2)
{
    array2[0]=-1;
}

PS. Thanks for help

1
  • What have you tried so far? Hint: Arrays are already passed as a reference in JavaScript, so just forget the pointers in the C code and it should be straightforward to convert it to JavaScript. Commented Dec 13, 2010 at 4:55

1 Answer 1

1

I think something like this ought to do the trick:

var myArray = [0, 1, 2];

function setDefault(ar) {
    setDefault2(ar);
}

function setDefault2(ar) {
    // do the thingy you want here
    ar[0]--;
}

// test
setDefault(myArray);
// myArray should contain [-1, 1, 2] now
Sign up to request clarification or add additional context in comments.

5 Comments

That's some dangerous use of for ... in over there. And by the way, the OP only wanted array[0] = -1.
Yeah. I just changed that. :)
Thank you, but it didn't help =(
You're assuming that the input array will always have 0 as the first element.
Yep. It's always first element of array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.