Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How to compare an address to another address? (http://www.go4expert.com/forums/compare-address-address-t21044/)

askmewhy25 19Feb2010 16:19

How to compare an address to another address?
 
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);
    }


abubacker 19Feb2010 17:10

Re: How to compare an address to another address?
 
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.

askmewhy25 19Feb2010 17:24

Re: How to compare an address to another address?
 
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:

virxen 19Feb2010 22:48

Re: How to compare an address to another address?
 
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;
      char hexAddress[10];
      long int address;
      int newValue=0;

    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);

    }
    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]);
    }

    getchar();    getchar();
return 0;
}


askmewhy25 20Feb2010 15:05

Re: How to compare an address to another address?
 
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);
    }


virxen 20Feb2010 16:58

Re: How to compare an address to another address?
 
try changing

Quote:

case 1:
main();
break;
to

Code:

case 1:
    //main();
  break;


askmewhy25 20Feb2010 20:22

Re: How to compare an address to another address?
 
Ahh hahaha..now I got it because whenever i choose the number 1 the program resets..
Thanks alot virxen


All times are GMT +5.5. The time now is 06:54.