Check whether a pointer points to a empty space on a int array
So, I've got a assignment that asks for me to compare 2 arrays of integers
(a[5] and b[8]) and check for repeated elements using pointers. So far,
here's the code I came up with:
int main(void) {
int *pa, i, j, sizeA=5, sizeB=8, a[sizeA], b[sizeB], aux[sizeB];
for (i=0; i<sizeA; i++){
scanf("%d", &a[i]);
}
for (i=0; i<sizeB; i++){
scanf("%d", &b[i]);
}
for (i=0; i<sizeB; i++){
aux[i] = NULL;
}
for(i=0;i<sizeA; i++){
for(j=0; j<sizeB; j++){
if ((a[i] == b[j]))
aux[i] = b[j];
}
}
for(i=0;i<sizeA; i++){
pa = &aux[i];
if ((pa != NULL)&&(*pa!=aux[i+1])){
printf("%d \n", *pa);
}
}
return (EXIT_SUCCESS);
}
It got compiled and run without errors, but when there are no repeated
elements, the last for Loop prints strange values such as 435304.
I tried to make the pointer "pa" scan through the array "aux", and only
print elements that are different from the next one on the array, and
check if the position the pointer is pointing is empty.
Any tips?
EDIT: I solved it initializing the aux array with NULL values. Does it
count as a valid solution? Is there a better way to do it?
No comments:
Post a Comment