-1

My previous question was answered but I have another in the same vein. Why does the code not work when I try to use a multi-dimensional array?

void change(int *);

int main(void){
    int array[1][2] = {2};

    printf("%d", array[1][2]);
    change(array);
    printf("%d", array[1][2]);
}

void change(int *array){
    array[1][2] = 4;
}
  • 2
    `change` takes an `int` argument, not `int *`, so that'a a deal breaker right out of the gate. Second, `*array[0]` is certainly wrong, even after fixing the parameter type to proper `int *`. It would be `array[0] = ...` *or* `*array = ...` to set the first element, but not both. I think a thorough review of arrays, function parameters and passing the former as arguments to the latter, are on your upcoming agenda. – WhozCraig May 25 '19 at 17:12
  • From point of view inside of `change(int)`, what would make the parameter "array" an array, apart from the name which the compiler ignores? – Yunnosch May 25 '19 at 17:13
  • Possible duplicate of [How to pass a multidimensional array to a function in C and C++](https://stackoverflow.com/questions/2828648/how-to-pass-a-multidimensional-array-to-a-function-in-c-and-c) – dandan78 May 25 '19 at 17:41
  • Now you've completely altered the type of array declared (was `int[2]`, now its `int[1][2]`. Further, your two `printf` commands are both using invalid indexes on the `array` variable. C arrays of dim `n` are indexed using `0..(n-1)`. – WhozCraig May 26 '19 at 00:00

3 Answers3

1

You need to use asterisks for parameter at the time of declaration and definition. For making array[0]=4, you only need to assign 4 to array[0] without asterisk. So your code should be:

void change(int *);

int main(void){
    int array[1] = {2};

    printf("%d", array[0]);
    change(array);
    printf("%d", array[0]);
}

void change(int *array){
    array[0] = 4;
}
hg__
  • 38
  • 6
1

enter code here void change(int*);

int main(void){
    int array[1] = {2};

    printf("%d\n", array[0]);
    change(array);
    printf("%d\n", array[0]);
}

void change(int* array)
{
    array[0] = 4;
    *(array + 0) = 4;
}

don't put '&' in array. it is pointer. so you must change your function "change(int ...) to change(int* )"

and to change array[0] you don't need to put * on array. it's an array!. or to use * . then you must add n-th element number.

0

C language uses pass by value.
So, You have to receive the address of array as argument.

void change(int *, int);

int main(void) {
    int array[3] = { 1, 2, 3 };

    printf("%d", array[1]);
    change(array, 1);
    printf("%d", array[1]);
}

void change(int *array, int index) {
    *(array+index) = 4;
}

For the multi dimension, You should pass a size of column.
I made 2 samples as change and change2.
I think you can understand change well.
But, In case of change2, array has a head address of memory in array[2][3] and it has 6 integers by 2x3. So, you can calculate.

void change(int array[2][3], int, int, int);
void change2(int *, int, int, int, int);

int main(void) {
    int array[2][3] = { { 1, 2, 3 }, {4, 5, 6} };

    printf("%d", array[1][1]);
    change(array, 1, 1, 10);
    printf("%d", array[1][1]);
    change2((int *)array, 3, 1, 1, 20);
    printf("%d", array[1][1]);
}

void change(int array[2][3], int row, int col, int value) {
    array[row][col] = value;
}

void change2(int *array, int size_col, int row, int col, int value) {
    *(array + row*size_col + col) = value;
}
yaho cho
  • 1,779
  • 1
  • 7
  • 19