Alright, I'm relatively new to C but not to programming. What I'm trying to do is create a struct, pass it to another function that will allocate memory for it and fill it.
Here's an example of what I'm doing, which creates exactly the same situation that I am dealing with
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct _dog {
char age;
char weight;
} dog;
void makenewdog(dog * dogtomake) {
dogtomake = (dog *) malloc(sizeof(dog));
dogtomake->age = 1;
dogtomake->weight = 5;
/* this prints 5, which is correct */
printf("Ok, I made you a dog. It's %d pounds.\n", dogtomake->weight);
}
int main (int argc, const char * argv[]) {
dog doggie;
doggie.weight = 0; doggie.age = 0;
makenewdog(&doggie);
/* weight should now be 5, but it returns 0. doggie's pointer isn't changed to
respect what it was given in makenewdog either */
printf("I have a dog now, %d pounds.\n", doggie.weight);
return 0;
}
Any pointers (no pun intended) in the right direction would be a great help, thanks!