-3

I'm using ANSI C 89. I would like to write a function that takes a two-dimensional array as a parameter and modifies it. Possibly without using structures or pointers. My code is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define level 0
#define elements 1

void merge( int state[30][2] ){
  int index_state = 0;
  int array[30][2];
  int index_array = 0;
  memset (array, -1, sizeof(array));
  for ( index_state = 0; state[index_state][level] != -1; index_state++ ){
    if ( state[index_state][level] != 4 ){
      if ( index_state == 0 ){
        array[0][level] = state[index_state][level];
        array[0][elements] = state[index_state][elements];
      }
      else {
        if ( state[index_state][level] == array[index_array][level] ){
          array[index_array][elements] += state[index_state][elements];
        }
        else{
          index_array++;
          array[index_array][level] = state[index_state][level];
          array[index_array][elements] = state[index_state][elements];
       }      
     }
   }
 }
 state = array;
 int i = 0;

 printf("-----\nPrint values array inside function merge\n------\n");

 for ( i = 0; array[i][level] != -1 ; i++ ){
   printf("Level merge[%d]: %d, elements merge[%d]: %d.\n", i, array[i][level], i, array[i][elements]);
 }

 printf("-----\nPrint values state inside function merge\n------\n");

 for ( i = 0; state[i][level] != -1 ; i++ ){
   printf("Level merge[%d]: %d, elements merge[%d]: %d.\n", i, state[i][level], i, state[i][elements]);
 }
 }

int main(void) {
  int start_state[30][2] = {{0,4},{1,4},{2,32},{2,32},{4,64},{3,64},{2,64},{1,8},{2,8},{3,48},{4,64},{3,64},{2,4},{3,4},{4,12},{3,12},{2,16},{1,8},{-1,-1}};
  int i = 0;
  printf("-----\nPrint start_state.\n-----\n");
  for ( i = 0; start_state[i][level] != -1; i++){
    printf("start_state[%d][level] = %d, start_state[%d][elements] = %d.\n",i,start_state[i][level],i,start_state[i][elements]);
  }
  merge ( start_state );
  printf("-----\nPrint the merged start_state.\n-----\n");
  for ( i = 0; start_state[i][level] != -1; i++){
    printf("start_state[%d][level] = %d, start_state[%d][elements] = %d.\n",i,start_state[i][level],i,start_state[i][elements]);
  }
  return EXIT_SUCCESS;
}

The function takes the input correctly and does what I want it does. When I look at what the function prints everything's gone alright. When inside the function I print the internal array and the input modified I get the same result. But when it comes to the main function to print it looks like nothing happened. Here you are the output of the source code above.

-----
Print start_state.
-----
start_state[0][level] = 0, start_state[0][elements] = 4.
start_state[1][level] = 1, start_state[1][elements] = 4.
start_state[2][level] = 2, start_state[2][elements] = 32.
start_state[3][level] = 2, start_state[3][elements] = 32.
start_state[4][level] = 4, start_state[4][elements] = 64.
start_state[5][level] = 3, start_state[5][elements] = 64.
start_state[6][level] = 2, start_state[6][elements] = 64.
start_state[7][level] = 1, start_state[7][elements] = 8.
start_state[8][level] = 2, start_state[8][elements] = 8.
start_state[9][level] = 3, start_state[9][elements] = 48.
start_state[10][level] = 4, start_state[10][elements] = 64.
start_state[11][level] = 3, start_state[11][elements] = 64.
start_state[12][level] = 2, start_state[12][elements] = 4.
start_state[13][level] = 3, start_state[13][elements] = 4.
start_state[14][level] = 4, start_state[14][elements] = 12.
start_state[15][level] = 3, start_state[15][elements] = 12.
start_state[16][level] = 2, start_state[16][elements] = 16.
start_state[17][level] = 1, start_state[17][elements] = 8.
-----
Print values array inside function merge
------
Level merge[0]: 0, elements merge[0]: 4.
Level merge[1]: 1, elements merge[1]: 4.
Level merge[2]: 2, elements merge[2]: 64.
Level merge[3]: 3, elements merge[3]: 64.
Level merge[4]: 2, elements merge[4]: 64.
Level merge[5]: 1, elements merge[5]: 8.
Level merge[6]: 2, elements merge[6]: 8.
Level merge[7]: 3, elements merge[7]: 112.
Level merge[8]: 2, elements merge[8]: 4.
Level merge[9]: 3, elements merge[9]: 16.
Level merge[10]: 2, elements merge[10]: 16.
Level merge[11]: 1, elements merge[11]: 8.
-----
Print values state inside function merge
------
Level merge[0]: 0, elements merge[0]: 4.
Level merge[1]: 1, elements merge[1]: 4.
Level merge[2]: 2, elements merge[2]: 64.
Level merge[3]: 3, elements merge[3]: 64.
Level merge[4]: 2, elements merge[4]: 64.
Level merge[5]: 1, elements merge[5]: 8.
Level merge[6]: 2, elements merge[6]: 8.
Level merge[7]: 3, elements merge[7]: 112.
Level merge[8]: 2, elements merge[8]: 4.
Level merge[9]: 3, elements merge[9]: 16.
Level merge[10]: 2, elements merge[10]: 16.
Level merge[11]: 1, elements merge[11]: 8.
-----
Print the merged start_state.
-----
start_state[0][level] = 0, start_state[0][elements] = 4.
start_state[1][level] = 1, start_state[1][elements] = 4.
start_state[2][level] = 2, start_state[2][elements] = 32.
start_state[3][level] = 2, start_state[3][elements] = 32.
start_state[4][level] = 4, start_state[4][elements] = 64.
start_state[5][level] = 3, start_state[5][elements] = 64.
start_state[6][level] = 2, start_state[6][elements] = 64.
start_state[7][level] = 1, start_state[7][elements] = 8.
start_state[8][level] = 2, start_state[8][elements] = 8.
start_state[9][level] = 3, start_state[9][elements] = 48.
start_state[10][level] = 4, start_state[10][elements] = 64.
start_state[11][level] = 3, start_state[11][elements] = 64.
start_state[12][level] = 2, start_state[12][elements] = 4.
start_state[13][level] = 3, start_state[13][elements] = 4.
start_state[14][level] = 4, start_state[14][elements] = 12.
start_state[15][level] = 3, start_state[15][elements] = 12.
start_state[16][level] = 2, start_state[16][elements] = 16.
start_state[17][level] = 1, start_state[17][elements] = 8. 

What should I edit to make the changes in the original array persistent? I had a look on the Internet and sometimes it seems this way it should work (isn't the second answer to this question Passing modified array values back to main function in C similar to my case except for the two-dimensional array ?) and sometimes it looks like I must refer to the array using a pointer.

3
  • 1
    You cannot pass an array to/from a function in C. Only a pointer. And you hardly can do anything useful witha array either without using a pointer. Not even access an element. What do you want to accomplish? Provide a minimal reproducible example (concentrate on the Minimal!). Commented Mar 3, 2016 at 15:46
  • Modifying variable of caller without pointer in C... Humm pretty sure that is impossible Commented Mar 3, 2016 at 15:53
  • You can't do it without pointers because with no pointers, your function will receive a COPY of the value. You can change the content, but once your function end, the copy gets destroyed, and the original will be still intact Commented Mar 3, 2016 at 16:17

1 Answer 1

0

In merge, you are making changes to the local variable array. It makes sense you don't see the modifications to array in the calling function.

The line

 state = array;

does not copy the conents of array to state. It merely changes where state points to in the function.

Change merge to modify state itself and you will see the changes visible in the calling function.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh god, I thought it should have been something stupid but not this stupid. Many thanks.

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.