Reputation: 13
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:
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]);
}
}
Upvotes: 1
Views: 1840
Reputation: 15232
what you are required to do is let the user enter a number, and interpret it as a pointer. First you have to check the pointer, next you have to use it. If you need to interpret a number as a pointer, you need a typecast. To make it easier for yourself, you should do this once, and use the result both for the checking and the assignment.
int temp;
scanf("%d", &temp);
Now you have the address in temp
. First typecast this to a pointer;
int *tempptr;
tempptr = (int*)temp; // compiler will issue a warning for this.
Note this can only work if the size of an int
is identical to the size of int *
, you can generate a compiler error if it is not:
#if sizeof(int) != sizeof(int *)
#error cannot typecast int to int *
#endif
I believe the way you check was correctly, but you can now use tempptr
instead
//if((int*)temp == &data[i])
if ( tempptr == &data[i])
This is strange:
int var = 4;
int* pVar;
pVar = &var;
*pVar = temp; // this means: var = temp
Here you are using temp no longer as a pointer, but as an integer again.
If you use tempptr
you don't need pVar
and var
.
The scanf
will probably do what you need, but it is very confusing as you are doing an implicit typecast;
scanf("%d", *pVar);
*pVar
is an int, but scanf
expects an int *
. If you would really want this, it requires a decent comment explaing to others who read the code what you are doing here. Without it, anybody else would simply mark this line as wrong.
If you use tempptr instead, you use an int *
where scanf expects an int *
scanf("%d", tempptr);
Upvotes: 1