I have 2 language codes coming in the stream. I’m storing this in a 3 byte char array(unsigned char a[3]). I wanted to compare it with another value stored in a pointer(unsigned char *c).The array a[3] is stored inside a structure(struct s[2]) to get the multiple datas – Is this correct as i’m little confused as array – const pointer cannot be made to point to another location as it is already pointing to a location. But including the array inside a structure and making the zeorth element of the structures array to point to one location and the 1 st element of the structures array is possible. Is the understanding i have is correct.
I wanted to store the 2 array values.So I have declared a structure inside which i have declared the 3 byte char array.Is this way of doing is correct. Is there alternate way to do it.
EDITED:
#include<stdio.h>
int main(){
int i,flag=0,count=0;
struct n{
unsigned char b[3];
};
unsigned char *d=NULL;
struct s{
unsigned char *a;
};
struct s m[2];
struct n w[2];
// memcpy(w[0].b,"eng",sizeof("eng"));
// memcpy(w[1].b,"fre",sizeof("fre"));
strcpy(w[0].b,"eng");
strcpy(w[1].b,"fre");
d = w[1].b; // current lang
m[0].a = w[0].b; // storing the 2 lang in a pointer inside a structure
m[1].a = w[1].b;
i=0;
printf("\nm[0].a:%s\n",m[0].a);
printf("\nm[1].a:%s\n",m[1].a);
printf("\nw[0].b:%s\n",w[0].b);
printf("\nw[1].b:%s\n",w[1].b);
while((m[i].a) && d){ // And comparing
if(m[i].a++ != d++){
flag =1; //if strings are unequal break;
break;
}
i++;
}
if(flag){
printf("Not equal\n");
}
else{
printf("\nEqual\n");
flag =0;
}
return 0;
}
o/p:
m[0].a:engfre
m[1].a:fre
w[0].b:engfre
w[1].b:fre
Not equal
But there s an mistake it shows un equal . Is this way of storing the arrays in a pointer inside a strucutre is correct method. or is there any other way to do this.
EDIT:
I wanted to compare the 2 strings. The 2 strings are equal but i’m getting it as unequal.
Is the pointer a need to be stored in a structure to store the 2 arrays or is there another way of dong this.
1 Answer