This is a code I testing to copying struct.
1 #include <stdio.h>
2 #include <string.h>
3
4 typedef struct emp_struct {
5 char *name;
6 int employee_no;
7 float salary,
8 tax_to_date;
9 } Employee;
10
11 typedef Employee Database[10];
12
13 Database people = {
14 {"Fred", 10, 10000, 3000},
15 {"Jim", 9, 12000, 3100.5},
16 {"Fred", 13, 1000000, 30},
17 {"Mary", 11, 170000, 4000},
18 {"Judith", 45, 130000, 50000},
19 {"Nigel", 10, 5000, 1200},
20 {"Trevor", 10, 20000, 6000},
21 {"Karen", 10, 120000, 34000},
22 {"Marianne", 10, 50000, 12000},
23 {"Mildred", 10, 100000, 30000}
24 };
25
26 int main () {
27 // array act like pointer, thus pointing + pointing = ERROR
28 printf("people[1]->name: ERROR\n");
29 // jump its memory amount of struct size
30 printf("(people+1)->name:%s\n",(people+1)->name);
31 // array works as a pointer
32 printf("people[3].name:%s\n",people[3].name);
33
34 /* Is it possible to assign struct to struct? */
35 printf("\nAssigning struct to struct\n");
36 Employee temp;
37 temp = *(people+5); // Nigel Record
38 printf("Name: %s\n",temp.name);
39 // exchange
40 strcpy(temp.name, "Ahn");
41 printf("Changed: %s\n",temp.name);
42 printf("Original: %s\n",people[5].name);
43
44 return 0;
45 }
When I tried to strcpy(new, string) in line 40, then it throws out Bus error: 10.
I expected that value Changed and Original are same in line 41 and 42. But it would not work. What is the problem of assigning?
I knew that Bus error:10 is occurred by lack of assignment space. But my name field in struct is pointer (in line 5). If I changed name field like
char name[100];
It works properly and Changed and Original value are different! Even though I assigned that as a pointer.
What is the problem of this struct assigning?