stephen stephen - 1 month ago 6
C Question

Value at Array Index 0 keeps resetting even when it is in the scope of the block it is defined

I wrote a simple program in C whose function is to add or remove a number in the integer array of size 3.All the members of the array are initialized to -1.

The user has to select the appropriate option from the menu and can insert ,remove,modify or view the elements of the array.Following are the steps I followed after compiling the program.


  1. Selected option 1 to add the element.(Here, the program will first look for a negative number in the array and then replace the negative number with the number provided by the user say for eg:2.so here the 1st element of the array should be overwritten by 2 i.e.
    array[0] = 2
    )

  2. selected option 4 to view the elements of the modified array.



To my surprise,the first element of the array was 0.I thoroughly checked the entire program for accidental modification of the 1st element in the array but couldn't find anything.I am unable to figure out what is the issue.

Note:The code has been compiled on linux platform(OpenSUSE).I have put some debug printf's in the code.Also,I found out that when I changed the location of the declaration of the array
"int array[3] = {-1,-1,-1};
" and put it at the end of all the declaration of the variables,the program worked as intended.But I would still like to know the reason of the failure

#include <stdio.h>
unsigned char ShowMenu();

int main()
{
ShowMenu();
return 0;
}

unsigned char ShowMenu()
{
int array[3] = {-1,-1,-1};
unsigned char Operation = 0;
int Number = 0;
unsigned char Index = 0;

do
{
printf("number atlocation 1 is %d\n",array[0]);
printf("Input Operation to be performed\n");
printf("1.Add\n2.Remove\n3.Modify\n4.View all\n5.Exit\n");
scanf("%d",&Operation);
printf("number atlocation 1 is %d\n",array[0]);
switch(Operation)
{
case 1:
printf("enter a positive number\n");
scanf("%d",&Number);
//check if the number is positive
if(Number > 0)
{
for(Index = 0;Index < 3;Index++)//check for neg number positions
{
if (array[Index] < 0)
{
array[Index] = Number;
printf("Number added at location %d\n",Index);
break;
}
else
{
if (Index == 2)
{
printf("No more elements can be added\n");
}
else
{
//do nothing
}
}
}
}
else
{
printf("the number is negative\n");
}
Index = 0;
Number = 0;
printf("leaving case 1 :%d\n",array[0]);

break;

case 2:

printf("Input the number to remove\n");
scanf("%d",&Number);
for(Index = 0;Index < 3;Index++)//check for neg number positions
{
if (array[Index] == Number)
{
array[Index] = -1;
printf("Number removed at location %d\n",Index);
break;
}
else
{
if(Index == 2)
{
printf("Number couldn be found\n");
}
else
{
//do nothing
}
}
}
Index = 0;
Number= 0;
break;

case 3:
printf("Input the ponumber to find and replace\n");
scanf("%d",&Number);
//this is not complete
break;


case 4:
printf("The numbers in the array are\n",);
for(Index = 0;Index < 3;Index++)//check for neg number positions
{
printf("Index %d : %d\n",Index,array[Index]);
}
Index = 0;
Number= 0;
break;

case 5:
printf("Operation complete\n");
break;

default:
printf("Invalid input\n");
}

}
while(Operation != 5);

return Operation;

}

Answer

You have

unsigned char Operation = 0;

and you use

scanf("%d", &Operation);

therefore you need

int Operation = 0;

The %d format specifier requires a pointer to int but you provided a pointer to unsigned char.

Using scanf when the arguments don't match the format string yields in undefined behaviour. Your compiler should have warned you about this if you'had compiled with the -Wall option.

BTW: the code you posted doesn't compile:

case 4:
  printf("The numbers in the array are\n", );
                                         ^ problem here
Comments