c - 多维数组不保存值

标签 c arrays multidimensional-array

我一直想看看问题出在哪里,但找不到。每次,数组内存的值都给我 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, &registerX, 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/

相关文章:

arrays - 查看特定属性的所有记录并查看最高值

ios - 如何在 Swift 中创建一个空数组?

c++ - 打印多维数组时输出错误

c - Keil Arm 编译器 : Is there a way to hook in the same function for two interrupts without modifying the Interrupt Vector Table?

c - 带有 libcurl 的 HTTP 状态代码?

c++ - 求解数组中的方程

arrays - 在 F# 中访问 GetSlice 方法

c++ - 使用 C++ 的二维数组

c - 如何通过链表编写dequeue的函数dequeue

c - 理解全局变量的概念