如何使用 C 函数修改数组
How to modify an array using a C function
我正在使用 ANSI C 89。
我想写一个函数,将一个二维数组作为参数并修改它。可能不使用结构或指针。
我的代码是:
#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;
}
该函数正确接受输入并执行我希望它执行的操作。当我查看函数打印的内容时,一切正常。在函数内部时,我打印内部数组并修改输入,得到相同的结果。但是当涉及到打印的主要功能时,它看起来什么也没发生。
这是上面源代码的输出。
-----
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.
我应该编辑什么才能使原始数组中的更改持久化?我在互联网上看了看,有时它似乎应该这样工作(这个问题的第二个答案 Passing modified array values back to main function in C 除了二维数组之外与我的情况相似吗?)有时它看起来像我必须使用指针引用数组。
在 merge
中,您正在更改局部变量 array
。在调用函数中看不到对 array
的修改是有道理的。
行
state = array;
不会将 array
的内容复制到 state
。它只是改变 state
在函数中指向的位置。
更改 merge
以修改 state
本身,您将在调用函数中看到可见的更改。
我正在使用 ANSI C 89。 我想写一个函数,将一个二维数组作为参数并修改它。可能不使用结构或指针。 我的代码是:
#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;
}
该函数正确接受输入并执行我希望它执行的操作。当我查看函数打印的内容时,一切正常。在函数内部时,我打印内部数组并修改输入,得到相同的结果。但是当涉及到打印的主要功能时,它看起来什么也没发生。 这是上面源代码的输出。
-----
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.
我应该编辑什么才能使原始数组中的更改持久化?我在互联网上看了看,有时它似乎应该这样工作(这个问题的第二个答案 Passing modified array values back to main function in C 除了二维数组之外与我的情况相似吗?)有时它看起来像我必须使用指针引用数组。
在 merge
中,您正在更改局部变量 array
。在调用函数中看不到对 array
的修改是有道理的。
行
state = array;
不会将 array
的内容复制到 state
。它只是改变 state
在函数中指向的位置。
更改 merge
以修改 state
本身,您将在调用函数中看到可见的更改。