Omar shaaban
Omar shaaban

Reputation: 257

check if string changed

oK so I am proggramming in c and want to check if string changed so at first i did this :

 if(strcmp (ax,result)!=0){
    result=ax;
        xil_printf(result);
        xil_printf("detected");
}
    }

it detects only 1 time , then i figured it out that I am making the 2 pointers equal so then even if the piontee of ax changed so will happen to result since they both point at the same thing now , but i didnt want that i only wanted to change the data of result to be equal to of ppointee of ax as string ax will change later in the code so i can detect when it does. So i tried this :

if(strcmp (ax,result)!=0){
    *result=*ax;
        xil_printf(result);
        xil_printf("detected");
}
    }

and it came out with errors , anyway how to do what i want to do that i make data of result requal to ax but they are not pointing to the same thing: so if

ax-->"hello"  adrress: 232
result-->"frog"  adrress: 415

i detect they are diffren then i do this :

ax-->"hello"  adrress: 232
result-->"hello"  adrress: 415

BUT NOT LIKE THIS! :

ax-->"hello"  adrress: 232
result-->"hello"  adrress: 232   <--(they point at same thing which happens when i say result=ax)

So any ideas?

Upvotes: 0

Views: 487

Answers (1)

Keith Nicholas
Keith Nicholas

Reputation: 44308

you need to do strcpy(result, ax);

only thing is, you need to make sure result has enough room to store whats in ax

so your code will be

if(strcmp(ax,result) != 0){   // result is different from ax
     strcpy(result, ax);      // copy ax to result
     xil_printf(result);
     xil_printf("detected");
}      

Upvotes: 1

Related Questions