我一直想看看问题出在哪里,但找不到。每次,数组内存的值都给我 0,即使 int count 工作并继续计数为结构数据值,只有数组不保存该值。
#include<stdlib.h>
#include<stdio.h>
#define SIZE 1000
struct registers
{
int data;
} registerX;
void first(int *counter, struct registers* X1, int m[][2])
{
int value;
printf("Enter the value for the X\n");
scanf("%d", &value);
X1->data = value;
m[*counter][1] = X1->data;
*counter = ++*counter;
}
int main()
{
int memory[SIZE][2];
int choice;
int count = 0;
printf("Enter the instruction number:\n");
while(choice != 107)
{
scanf("%d", &choice);
if(choice == 101)
{
memory[count][0] = 101;
first(&count, ®isterX, memory);
printf("%d\n", memory[count][1]);
printf("%d\n", memory[count][0]);
}
}
}
最佳答案
您的代码存在一些问题。
首先,while(choice != 107)
中的choice
未初始化。要修复此问题,请使用 do...while
循环而不是 while
循环,因为您希望循环体在检查条件之前执行一次。
其次,*counter =++*counter;
未定义的行为具有 KerrekSB points out in his comment 。要修复它,请使用 (*counter)++;
。
最后,在最后两个 printf 中打印数组中未初始化的元素。原因是您在函数中递增了 *counter
,这也会修改 main
中的 count
,因为 counter
是指向count
地址的指针。
关于c - 多维数组不保存值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27203924/