int main()
{
unsigned char a[3];
unsigned char (*p)[3]=NULL;
unsigned char *q=NULL;
int i = 0;
a[0]=0;
a[1]=1;
a[2]=2;
p=&a;
for(i=0;i<3;i++){
if((*p)[3] == a[3]){
printf("*p[%d]:%d a[%d]:%d",i,(*p)[3],i,a[3]);
}
}
}
o/p:
*p[0]:0 a[0]:0*p[1]:0 a[1]:0*p[2]:0 a[2]:0
Exited: ExitFailure 14
I want to copy an array of size 3 to a pointer and to do a comparision. I have written a sample program. But i’m getting an error value.
I ran this using an online c compiler . (codepad.org)
Please help me in identifying my mistakes.
Your variable
pis an array, not a pointer. You can’t re-assign an array to point somewhere else, so the lineis not valid.
Also, C indexes from 0 as you seem to know, so comparisons using index
[3]for arrays of size 3 are not valid.Further, in your comparison loop you’re not actually using
ito index, but instead always comparing using the invalid constant index[3].It’s not very clear from your code (
qis not used, for instance), but it sounds as if you want to do something like:Of course, this will always print all the numbers, since
memcpy()will never fail to copy the memory. 🙂