Reputation:
I have been trying to see where the problem is but couldnt find it. Every time, the values of array memory gives me 0, even though int count works and keeps counting as the struct data value, only array does not save the value.
#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]);
}
}
}
Upvotes: 0
Views: 56
Reputation: 20244
You have some problems in your code.
Firstly,choice
in while(choice != 107)
is uninitialized. To fix it,use a do...while
loop instead of a while
loop as you want the loop body to execute once before checking the condition.
Secondly,*counter = ++*counter;
undefined behaviour has KerrekSB points out in his comment. To fix it,use (*counter)++;
.
Lastly,you print uninitialized elements of your array in the last two printf
s. The reason is that you increment *counter
in the function which will also modify count
in main
as counter
is a pointer which points to the address of count
.
Upvotes: 1