1

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]);
        }
}
wimh
  • 15,072
  • 6
  • 47
  • 98
Louie
  • 13
  • 4
  • 1
    This looks like it should be tagged C, not C++. – user657267 Nov 01 '14 at 12:06
  • looks like this line is your proble: `*pVar = temp;` – wimh Nov 01 '14 at 12:10
  • actually my problem is with this `(int*)temp==&data[i])` – Louie Nov 01 '14 at 12:13
  • 2
    Louie, very similar questions have been asked before, e.g. [here](http://stackoverflow.com/questions/3567905/c-is-it-safe-to-cast-pointer-to-int-and-later-back-to-pointer-again). The result of converting a user-inputted integer to pointer is somewhat unpredictable (i.e., it is implementation-defined). Therefore, it is not a good idea to let the user input addresses directly. Why don't you let the user simply chose from the list of addresses (i.e. when the user inputs "1", you use the first address in your list, for "2" the second, etc.)? – jogojapan Nov 01 '14 at 12:17
  • @jogojapan thnx for the help. But it is a requirement for our program in our programming class to input the address of the desired value to be changed. :( – Louie Nov 01 '14 at 12:22
  • Is the question for C or C++? The tag was C++ originally, but the code indeed looks like C. – jogojapan Nov 01 '14 at 12:28
  • I retagged it as `C` because I did not see anything which requires `C++` – wimh Nov 01 '14 at 12:38

1 Answers1

1

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); 
wimh
  • 15,072
  • 6
  • 47
  • 98
  • Side note for the OP: if you are willing to relax the requirement of storing the input address into an `int`, you should store it into a `uintptr_t`, which is guaranteed to be wide enough to hold an address. – Filipe Gonçalves Nov 01 '14 at 12:31
  • thnx! Ill try to fix my program with your suggestions :D – Louie Nov 01 '14 at 12:35
  • Can my program be done without the compiler issuing a warning?. If not, is having a warning bad or is it OK? – Louie Nov 01 '14 at 12:41
  • It might be possible to have the warning go away using a `#pragma`, but it depends which compiler you use. Casting an int to int* is normally never good, so basically you want a compiler message if you do something like this. So you should not disable the warning for the whole program, just that line. If you keep 0 warnings, you know there is something you need to look at when you get a warning. Otherwise you might completely forget about warnings, and run into strange bugs. – wimh Nov 01 '14 at 14:30