One way to do it is to have another array "mapping" to each value of the other array. Once you write a value to the first array, the value of the second in the same position turns to 1 (they should all be default 0).
Then, when checking for garbage values, if the position of the corresponding array is 0, then it's a garbage value.
See if this code helps:
#include<stdio.h>
#define ARRAY_SIZE 200
void insertIntoArray(size_t pos,char *x,int *xPoint,char value);
int main(void){
char x[ARRAY_SIZE];
int xPoint[ARRAY_SIZE];
//initialize pointer array
for(size_t i=0;i<ARRAY_SIZE;i++){
xPoint[i]=0;
}
//insert some values
insertIntoArray(0,x,xPoint,'A');
insertIntoArray(1,x,xPoint,'B');
insertIntoArray(2,x,xPoint,'C');
insertIntoArray(6,x,xPoint,'D');
insertIntoArray(9,x,xPoint,'E');
//check for garbage values
for(size_t i=0;i<ARRAY_SIZE;i++){
if(xPoint[i]==0){
printf("Garbage Value!\n");
}
else{
printf("%c\n",x[i]);
}
}
return 0;
}
void insertIntoArray(size_t pos,char *x,int *xPoint,char value){
x[pos]=value;
xPoint[pos]=1;
}
Here, I create two arrays with the same size. The second is just to map for the first one, hence I initialize all its values to 0.
Then, I created a function that inserts a value to the first array, turning the corresponding value of the second to 1.
Finally, I loop through the array; if the second's corresponding value is 0, the value of the first array is a garbage value; if it's 1, the value is legit.
The output is the following:
A
B
C
Garbage Value!
Garbage Value!
Garbage Value!
D
Garbage Value!
Garbage Value!
E
Garbage Value!
Garbage Value!
Garbage Value!
Garbage Value!
Garbage Value!
...