I have a pointer to a struct.
typedef struct anything{
char text[MAXI];
int any;
}Item;
After receiving the input in the struct, the program requests the user in which set he wants to store the struct.
void adding(){
Item *x = malloc(sizeof(Item));
printf("Enter an integer.");
scanf("%d", (&x->any));
printf("Enter a string.");
scanf("%s", (&x->text));
printf("Which set would you like to add the Item to A/B?");
char inp1 = 0;
while ((inp1=0),scanf("%s", &inp1) == 1 && (inp1 != 'A') && (inp1 != 'B')){
printf("Set does not exist\n");
}
if('A' == inp1)
add(A, x);
else
add(B, x);
flush();
instructs();
}
When receiving the input (in the while loop), the pointer x is being modified from 0x570ff8 to 0x57f00, which will then point to garbage instead of the input requested below.
Why is the pointer being altered?
Thank you.
The add function:
void add(Array *S,Item *x){
bool rep = false;
int i = 0;
for(i = 0; i<=(S->size); i++){
if(compStructs(*x,*(S->arr+i)))
rep = true;
}
if(rep == false){
x = realloc(S->arr, (S->size+1)*sizeof(Item));
(S->size)++;
printf("Item has been added");
}
else
printf("The item is already in the set.");
}