I need help on how to compare an address of a variable with an integer. //or a case of comparing pointers with integers.//
It is required that the program should have an array with initialized values. The program should display both the value and the address of the values. Then the program asks the user to input the address of the value the user wants to change. I want to display an error message like "Invalid Input!" when the program detects that the inputted address is not among the available addresses.
Sample output should be:
data[1] = 11
data[2] = 22
data[3] = 33
data[4] = 44
address[1] = 2538816
address[2] = 2538820
address[3] = 2538824
address[4] = 2538828
enter address: 2538888
Invalid Input!
I already created the code, it works, but it gives me a warning because i am casting it wrong( and I know that casting is not appropriate for what i need). My only focus is this:
- The user inputs an address to a int variable from the available addresses.
- the address that the user inputted should be compared to the available addresses. (this is the problem)
- if the inputted address is among the available ones, the user is to change the value stored on the address the user inputted.
- if not, the program should display Invalid Input!
also, a requirement for the program is that when the "Program asks the user to enter an address. The address is stored in a temporary int variable, and then copied to the integer pointer."
main()
{
int var = 4, temp;
int data[5]={11, 22, 33, 44};
int* pVar;
pVar = &var;
char choice;
void display(int, int*);
while(1)
{
display(var,data);
while(1)
{
printf("\nEnter address: ");
scanf("%d", &temp);
int check=0;
for(int i=0; i<4; i++)
{
if((int*)temp==&data[i])
{
check=1;
break;
}
}
if(check==1)
{
*pVar = temp;
break;
}
else
{
printf("Invalid Input!\n");
}
}
printf("Enter integer: ");
scanf("%d", *pVar);
display(var,data);
while(1)
{
getchar();
printf("\n\nDo you want to restart? [Y] Yes or [N] No: ");
scanf("%c", &choice);
if(choice=='Y'||choice=='y'||choice=='N'||choice=='n')
{
break;
}
else
{
printf("Invalid Input!\n");
}
}
if(choice=='N'||choice=='n')
{
break;
}
}//endline13
system("PAUSE");
}
void display(int var, int data[4])
{
system("cls");
printf("Values---------------------------*\n\n");
printf("var = %d\n\n", var);
for(int i=0; i<4; i++)
{
printf("data[%i] = %i\n", i, data[i]);
}
printf("\nAddresses---------------------------*\n\n");
printf("Address of variable = %d\n\n", &var);
for(int i=0; i<4; i++)
{
printf("Address of data[%i] = %i\n", i, &data[i]);
}
}