Hey everyone,
I'm teaching myself C and I am taking some time to practice structs. It has been something that has particularly confused me, mostly because I couldn't relate the concepts back to my introduction to the concept; through a silicon labs project file made for the efm32zg222f32. Anyway, I now have an understanding of what they are and how to use them.
SECTION A:
On to the problem: I was getting some really weird behaviour from the second instance of test_struct in the following code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
*
*/
struct test_struct {
int test1;
char test2;
char test3;
char test4[];
};
struct pointer_struct {
int* test1;
char* test2;
char* test3[10];
float test4;
};
int main(int argc, char** argv) {
char array[] = "test";
struct test_struct instance1;
struct test_struct instance2;
instance1.test1 = 8;
instance1.test2 = 'A';
instance1.test3 = '6';
strcpy(instance1.test4, "test string");
instance2 = instance1;
//strcpy(instance2.test4, "test string");
struct pointer_struct instance3;
instance3.test1 = &instance1.test1;
instance3.test2 = &instance1.test2;
printf("i1 test 1 is: %d \n", instance1.test1);
printf("i1 test 2 is: %c \n", instance1.test2);
printf("i1 test 3 is: %c \n", instance1.test3);
printf("i1 test 4 is: %s \n", instance1.test4);
printf("i2 test 1 is: %d \n", instance2.test1);
printf("i2 test 2 is: %c \n", instance2.test2);
printf("i2 test 3 is: %c \n", instance2.test3);
printf("i2 test 4 is: %s \n", instance2.test4);
printf("i3 test 1 is: %d \n", *instance3.test1);
printf("i3 test 2 is: %c \n", *instance3.test2);
return (EXIT_SUCCESS);
}
It turned out that the char array I had declared in test_struct needed to have it's length defined i.e. with a number in the square brackets. like so:
Code:
struct test_struct {
int test1;
char test2;
char test3;
char test4[12];
};
Otherwise I would get the following output:
Code:
i1 test 3 is: 6
i1 test 4 is: test string <<<<<
i2 test 1 is: 8
i2 test 2 is: A
i2 test 3 is: 6
i2 test 4 is: t <<<<<
i3 test 1 is: 8
i3 test 2 is: A
SECTION B:
Initially I thought that the string simply needed to be explicitly copied over, so I strcpy'd into the test4 array of the second instance like so:
Code:
instance2 = instance1;
strcpy(instance2.test4, "test string");
But that lead to this output:
Code:
i1 test 1 is: 1931506803
i1 test 2 is: t
i1 test 3 is: r
i1 test 4 is: ing
i2 test 1 is: 8
i2 test 2 is: A
i2 test 3 is: 6
i2 test 4 is: test string
i3 test 1 is: 1931506803
i3 test 2 is: t
First question: Could some kindly explain why the first instance of test_struct (discussed in section A) initialized to the appropriate size, even though it wasn't defined, but the second instance did not?
Second question: Why did I get garbage values in the output from the strcpy described in section B? I understand that because the size was undefined that it's inevitably going to spill over to other allocated space, but why did it do that in the first instance but not the second?
Thanks in advance.
Medicineman25