1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to compare an address to another address?

Discussion in 'C' started by askmewhy25, Feb 19, 2010.

  1. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    How to compare an address to another address and replaces the value stored in that address?

    Code:
    //This program prints the initialize value and the addresses of  the
    //elements inside the array. This proram also allows the user to change
    //the address of the hardcoded number in the program.
    
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define ARRAY_SIZE 4
    
    int main(){
        //Declare variable
        int x=1,rep;
        char error[128];
        // Declare array
        int arr[]={1,2,3,4};
        //Declare pointer
        int *ptr;
        
        system("cls");
        //Show value of array elements
        printf("The value of elements inside the array:\n\n");
        for(int i=0;i<ARRAY_SIZE;i++){
                printf("Value of element %d: %d\n",i,arr[i]);
        }
        //Print addresses of array elements
        printf("The addresses of elements inside the array:\n\n");
        for(int i=0;i<ARRAY_SIZE;i++){
        ptr = &arr[i];
            printf("Address of element %d: %p\n",i,ptr);
        }
        
        //Show value of variable
        printf("Value of x: %d\n",x);
        //Print address of variable
        ptr=&x;
        printf("Address of x: %p\n",ptr);
        
    
    This is the part that i really don't know. The user should in put any of the address printed by the program, after inputting the address the program asks the user to change the value stored in that address..

    //Input address
    int temp=0;
    printf("Please input an address: ");
    scanf("%d",&temp);

    *ptr = temp;

    //Print input
    printf("Your input: %p\n", *ptr);

    Code:
        printf("Please press the spacebar to continue!!");
        getche();
        system("cls");
        do{
           system("cls");
           printf("Do you want to repeat the computation?\n\n");
           printf("1 - Yes\n");
           printf("2 - No\n");
           scanf("%s",&error);
           rep=atoi(error);
           switch(rep){
                       case 1:
                            main();
                            break;
                       case 2:
                            break;
                       default:
                               system("cls");
                               printf("Invalid input. Please try again!!!\n");
                               printf("Please press the spacebar to continue!!");
                               getche();
                               break;   
                       }
           }while(rep>2||rep<1);
        } 
    
     
  2. abubacker

    abubacker New Member

    Joined:
    Feb 19, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I dont know whether you want to compare the memory location of its value,
    if you want to compare the address then the pointer variable itself holds the address it points to since it is numerical we can compare directly.

    if you want to compare based on its value then dereference the pointer and compare with it,

    I do not know whether I answered your question , If I am wrong please give me more details and please be specific.
     
  3. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    Let us just focus on this..:happy:
    Example:
    Blues inputted by the user

    Before:

    Position Value Address
    0 1 0022FEA0
    1 2 0022FEA4
    2 3 0022FEA8
    3 4 0022FEAC

    Please input an address: 0022FEA0
    Please input the new value for the address inputted: 7


    After

    Position Value Address
    0 7 0022FEA0
    1 2 0022FEA4
    2 3 0022FEA8
    3 4 0022FEAC

    Please input an address:
     
  4. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    this will help you a little bit.

    Code:
    //This program prints the initialize value and the addresses of  the
    //elements inside the array. This proram also allows the user to change
    //the address of the hardcoded number in the program.
    
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define ARRAY_SIZE 4
    
    int main(){
        //Declare variable
        int x=1,rep;
        char error[128];
        // Declare array
        int arr[]={1,2,3,4};
        //Declare pointer
        int *ptr;
          [COLOR="Red"]char hexAddress[10];
          long int address;
          int newValue=0;[/COLOR]
        system("cls");
        //Show value of array elements
        printf("The value of elements inside the array:\n\n");
        for(int i=0;i<ARRAY_SIZE;i++){
                printf("Value of element %d: %d\n",i,arr[i]);
        }
        //Print addresses of array elements
        printf("The addresses of elements inside the array:\n\n");
        for(int i=0;i<ARRAY_SIZE;i++){
        ptr = &arr[i];
            printf("Address of element %d: %p\n",i,ptr);
    
        }
        [COLOR="Red"]printf("\ngive address:");
        fgets(hexAddress,10,stdin);
        address = strtol (hexAddress,NULL,16);
        ptr=(int *)(address);
        printf("value of element at address=%p is =%d\n",ptr,*ptr);
        printf("\ngive new value:");
        scanf("%d",&newValue);
        *ptr=newValue;
        printf("value of element at address=%p is =%d\n",ptr,*ptr);
        for(int i=0;i<ARRAY_SIZE;i++){
                printf("Value of element %d: %d\n",i,arr[i]);
        }[/COLOR]
        getchar();    getchar();
    return 0;
    }
    
    
    
     
    Last edited: Feb 19, 2010
    shabbir likes this.
  5. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    Thank you!!:happy: but i already resolved the problem..
    now may problem is the looping at the last part because when i use
    switch statement the new value does not appear but when i use
    if statement the new values print

    Switch Statement Program
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define ARRAY_SIZE 4
    
    int main(){
        //Declare variable
        int x=1,rep,i,temp=0;
        char error[128];
        // Declare array
        int arr[]={2,3,4,5};
        //Declare pointer
        int *ptr;
        
        do{
       system("cls");
       //Show value of variable
        printf("Value of x: %d\n",x);
        //Print address of variable
        ptr=&x;
        printf("Address of x: %p\n\n",ptr);
      
        //Show value of array elements
        printf("The value of elements inside the array:\n\n");
        printf("Position\tElements\tAddresses\n\n");
        for(i=0;i<ARRAY_SIZE;i++){
                ptr=&arr[i];
                printf("%d:\t\t%d\t\t%p\n",i,*ptr,ptr);
        }
        
        //Input address and new value
        printf("\n");
        printf("Please input an address: ");
        scanf("%x",&ptr);
        printf("address\n: %X",ptr);
        printf("Please input the new value: ");
        scanf("%d",&temp);
        *ptr=temp;
        printf("address: %d\n",temp);
        
        printf("Please press the spacebar to continue!!");
        getche();
        while(rep>2||rep<1){
           system("cls");
           printf("Do you want to repeat the computation?\n\n");
           printf("1 - Yes\n");
           printf("2 - No\n");
           scanf("%s",&error);
           rep=atoi(error);
           switch(rep){
                       case 1:
                            main();
                            break;
                       case 2:
                            return 0;
                            break;
                       default:
                               system("cls");
                               printf("Invalid input. Please try again!!!\n");
                               printf("Please press the spacebar to continue!!");
                               getche();
                               break;   
                       }
           }
           }while(rep==1);
        }
    

    If Statement Program

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define ARRAY_SIZE 4
    
    int main(){
        //Declare variable
        int x=1,rep,i,q,temp=0;
        char error[128];
        // Declare array
        int arr[]={2,3,4,5};
        //Declare pointer
        int *ptr;
        
        do{
       system("cls");
       //Show value of variable
        printf("Value of x: %d\n",x);
        //Print address of variable
        ptr=&x;
        printf("Address of x: %p\n\n",ptr);
       
        //Show value of array elements
        printf("The value of elements inside the array:\n\n");
        printf("Position\tElements\tAddresses\n\n");
        for(i=0;i<ARRAY_SIZE;i++){
                ptr=&arr[i];
                printf("%d:\t\t%d\t\t%p\n",i,*ptr,ptr);
        }
        
        //Input address and new value
        printf("\n");
        printf("Please input an address: ");
        scanf("%x",&ptr);
        printf("address: %X\n",ptr);
        printf("Please input the new value: ");
        scanf("%d",&temp);
        *ptr=temp;
        printf("address: %d\n",temp);
        
        printf("Please press the spacebar to continue!!");
        getche();
            while(q!=1){
            if(q==2){system("cls");
            printf("Input must be either 1 or 2\n");}
            printf("\n\nReturn to view new list or quit?\n");
            printf("\n[1] Return\n[2] Quit\n");
            scanf("%d",&rep);
            if(rep==1)q=1;
            if(rep==2)return 0;
            if(rep!=1&&rep!=2)q=2;
            }//while q!=1
            q=0;
           }while(rep==1);
        }
    
    
     
  6. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    try changing

    to

    Code:
    case 1:
        //main();
       break;
    
     
  7. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    Ahh hahaha..now I got it because whenever i choose the number 1 the program resets..
    Thanks alot virxen
     

Share This Page