I have created a struct which stores an integer, and then in a method I set the integer to a value. However, when I try to access the value in any other method, I'm given a large value that isn't the same.
typedef struct thing {
int size;
arraylist** list; // defined elsewhere
} thing;
// Creates a thing
void create (thing* table) {
table = (thing *)malloc(sizeof(thing) * 8);
table->size = 1;
printf("%d", table->size); // prints 1
}
void another (thing* table) {
printf("%d", table->size); // prints a large number
}
void main() {
struct thing a;
create(&a);
printf("test: %d", (&a)->size); // prints a random large number, ex. 1667722352
another(&a);
}
sizeis defined intable = (thing *)malloc(sizeof(thing) * size);thing a;(no need for struct) is on the stack, and you're trying to allocate memory for it increate(). Remove malloc line, and it may work.aincreate(&a)is not used.