int main(int argc, char **argv) { printf("sizeof ary=%d\n", sizeof(ary)); printf("sizeof foo=%d\n", sizeof(struct foo)); //fun2(fee); //(int *)(fee.data[0]) = 55; //(char **)(fee.data[0]) = "hello"; //(char **)(fee.data[sizeof(int)]) = "hello"; //(char *)(fee.data[sizeof(int) + sizeof(char *)]) = 'k'; //(double *)(fee.data[sizeof(int) + sizeof(char *) + sizeof(double)]) = 178.213; //((foo_fun)function)(fee); bee.arg1 = 55; bee.arg2 = "hello"; bee.arg3 = 'k'; bee.arg4 = 178.213; ((bar_fun)function)(bee); { int offset = 0; poke_int(&fee, &offset, 66); poke_ptr(&fee, &offset, "goodbye"); poke_char(&fee, &offset, 'j'); poke_double(&fee, &offset, 91.3171); } ((foo_fun)function)(fee); }
int main() { bank *b = malloc(sizeof(bank)); init_bank(b,100); poke_int(b,97); poke_int(b,100); printf("%d\n",(int)*b->data); printf("%d\n",peek_int(b,1)); poke_string(b,"Hello World!"); printf("%s\n",&(b->data[4])); delete_bank(b); /* I need to learn why when passing bank *b; to a function, then inside the function calling malloc at * that pointer, the memory wasn't being allocated * * Aha! I should of known the answer to this.. Obvious we get a copy of the variables passed to the function * and the case is the same for pointers. So the local copy gets the allocated memory and the original doesnt * know about it. * * There are ways around it, pass in a pointer to a pointer I want to allocate * * void init(bank **b){ * *b = (bank*) malloc(sizeof(bank)); * (*b)->xxx = 0; * } * * or obviously allocate a tmp variable inside and return that. * * */ return 0; }