Pass structure by reference(?)
Hello everyone.
Long time reader, first time poster here. I had been interested in studying C for a long time and just recently finally decided to do so. Also been going through the tutorial given here in this site and I have learned alot. Did some simple programs and decided to make something a bit more challenging.
What I created below is a simple program that pass structures by reference (I hope I worded it right). This is actually a simplified version of a part of a code that I just saw(which is how I understood it).
Code:
#include <stdio.h>
#include <string.h>
typedef struct
{
struct numbers
{
int num1;
int num2;
int num3;
} num;
struct letters
{
char let1[2];
char let2[2];
char let3[2];
} let;
} holder;
typedef struct
{
struct numbers2
{
int num1;
int num2;
int num3;
} num;
struct letters2
{
char let1[2];
char let2[2];
char let3[2];
} let;
} holder2;
typedef struct
{
holder *struct1;
holder2 *struct2;
} allstruct;
void addnum(allstruct* addold, allstruct* addnew)
{
addnew->struct1->num.num1 = (addold->struct1->num.num1) + 1;
addnew->struct1->num.num2 = (addold->struct1->num.num2) + 2;
addnew->struct1->num.num3 = (addold->struct1->num.num3) + 3;
}
void pluslet(allstruct* oldlet, allstruct* newlet)
{
strcpy(newlet->struct1->let.let1, "b");
strcpy(newlet->struct1->let.let2, "c");
strcpy(newlet->struct1->let.let3, "d");
}
void change(allstruct* structold, allstruct* structnew)
{
addnum(structold, structnew);
pluslet(structold, structnew);
}
int main(void)
{
allstruct test;
test.struct1 = malloc(sizeof *test.struct1); <--added
test.struct1->num.num1 = 1;
test.struct1->num.num2 = 2;
test.struct1->num.num3 = 3;
strcpy(test.struct1->let.let1, "a");
strcpy(test.struct1->let.let2, "b");
strcpy(test.struct1->let.let3, "c");
printf("old num1: %u\n", test.struct1->num.num1);
printf("old num2: %u\n", test.struct1->num.num2);
printf("old num3: %u\n", test.struct1->num.num3);
printf("old let1: %s\n", test.struct1->let.let1);
printf("old let2: %s\n", test.struct1->let.let2);
printf("old let3: %s\n", test.struct1->let.let3);
allstruct newtest;
newtest.struct1 = malloc(sizeof *newtest.struct1); <--added
change(&test, &newtest);
printf("new num1: %u\n", newtest.struct1->num.num1);
printf("new num2: %u\n", newtest.struct1->num.num2);
printf("new num3: %u\n", newtest.struct1->num.num3);
printf("new let1: %s\n", newtest.struct1->let.let1);
printf("new let2: %s\n", newtest.struct1->let.let2);
printf("new let3: %s\n", newtest.struct1->let.let3);
free(test.struct1); <--added
free(newtest.struct1); <--added
return 0;
}
Now my problem is, when I compile the above code, everything is a-ok. (Also put -Wall flag on my compilation command just in case.) However, when I run the executable file, instead of printing the desired results on the screen, it produces a dump "core.xxxx" file. Now, I wouldn't just ask for help as long there something I can go by but thats just it, the compiler doesn't produce any warnings or errors. So now I'm stumped. I tried to search all over the web for a solution but I can't seem to find anything (most probably my google-fu is weak). So I ask if anyone can look over my code and point out what I did wrong. A clue would be fine also :D. Also feel free to criticize if you see anything bad in my work.
Thanks for any and all response.