1 00:00:07,260 --> 00:00:09,180 Let's talk about structs. 2 00:00:09,180 --> 00:00:12,130 Structs provide us with a way to group a bunch of variables together 3 00:00:12,130 --> 00:00:14,350 into a nice package. 4 00:00:14,350 --> 00:00:17,020 It's probably easiest to see an example right away, 5 00:00:17,020 --> 00:00:20,030 so we say struct, 6 00:00:20,030 --> 00:00:23,340 then opening curly brace, 7 00:00:23,340 --> 00:00:26,630 and in this struct, we'll have an int age, 8 00:00:28,920 --> 00:00:31,350 a char * name, 9 00:00:31,350 --> 00:00:34,670 and that's it. 10 00:00:37,350 --> 00:00:40,650 It may seem weird with a semicolon after a curly brace, 11 00:00:40,650 --> 00:00:43,620 but it's in fact necessary with structs. 12 00:00:43,620 --> 00:00:46,270 Any valid type can go within the struct definition. 13 00:00:46,270 --> 00:00:49,530 Here, we've used an int and a char *, 14 00:00:49,530 --> 00:00:52,610 but you could also use an array, of say, 100 elements 15 00:00:52,610 --> 00:00:54,910 or even another struct. 16 00:00:54,910 --> 00:00:56,960 When you're using structs in C, 17 00:00:56,960 --> 00:00:58,430 you're creating new types 18 00:00:58,430 --> 00:01:00,860 out of a collection of other types. 19 00:01:00,860 --> 00:01:02,620 Here, we're making a new type 20 00:01:02,620 --> 00:01:05,060 out of an integer and a char *. 21 00:01:05,060 --> 00:01:07,400 As we'll see later, a struct type 22 00:01:07,400 --> 00:01:10,700 is in a lot of ways equivalent to any other type you're used to. 23 00:01:10,700 --> 00:01:13,310 Usually, I'll be comparing how a struct type 24 00:01:13,310 --> 00:01:15,790 is similar to an integer type. 25 00:01:15,790 --> 00:01:18,520 While the code we wrote is valid C, 26 00:01:18,520 --> 00:01:20,320 it's not very useful, 27 00:01:20,320 --> 00:01:22,340 and clang will give us a warning. 28 00:01:22,340 --> 00:01:24,970 Remember how structs and its are similar? 29 00:01:24,970 --> 00:01:26,710 Well, we basically just said 30 00:01:27,840 --> 00:01:30,060 int, 31 00:01:30,060 --> 00:01:33,140 which isn't a very helpful line. 32 00:01:33,140 --> 00:01:35,760 So let's actually declare a variable of that type 33 00:01:35,760 --> 00:01:38,760 by giving it a name before the semicolon. 34 00:01:42,170 --> 00:01:45,000 We'll call the variable student. 35 00:01:48,190 --> 00:01:51,350 Now we've declared a variable called student 36 00:01:51,350 --> 00:01:53,980 with the type given by the struct. 37 00:01:53,980 --> 00:01:56,730 How do we get to the variables inside the struct? 38 00:01:56,730 --> 00:01:59,040 Technically, the names for these variables 39 00:01:59,040 --> 00:02:01,070 are members. 40 00:02:01,070 --> 00:02:04,000 To access any particular member in a student struct, 41 00:02:04,000 --> 00:02:06,440 you append a dot to the variable name, 42 00:02:06,440 --> 00:02:08,860 followed by the name of the member you want. 43 00:02:08,860 --> 00:02:11,690 So here, the only 2 valid possibilities 44 00:02:11,690 --> 00:02:17,760 are student.age 45 00:02:17,760 --> 00:02:24,460 and student.name. 46 00:02:24,460 --> 00:02:26,820 And we can do something like 47 00:02:26,820 --> 00:02:30,320 student.age = 12 48 00:02:30,320 --> 00:02:39,310 and student.name = student. 49 00:02:39,310 --> 00:02:42,580 Now what if we wanted to make a second student? 50 00:02:42,580 --> 00:02:44,760 You might think to copy and paste these lines 51 00:02:44,760 --> 00:02:48,110 and change student to student 2 or something, 52 00:02:48,110 --> 00:02:50,090 and that will work, 53 00:02:50,090 --> 00:02:52,670 but technically, student and student 2 54 00:02:52,670 --> 00:02:54,540 don't have the same type. 55 00:02:54,540 --> 00:02:56,940 See, you won't be able to assign them to one another. 56 00:02:56,940 --> 00:02:58,560 This is because, so far, 57 00:02:58,560 --> 00:03:00,950 your struct has been anonymous. 58 00:03:00,950 --> 00:03:02,290 We need to give it a name. 59 00:03:02,290 --> 00:03:04,420 To do that, we insert the name of the struct 60 00:03:04,420 --> 00:03:06,950 after the word struct. 61 00:03:09,440 --> 00:03:11,170 student, 62 00:03:11,170 --> 00:03:14,680 followed by the definition. 63 00:03:16,500 --> 00:03:18,940 We can still immediately declare a variable of type 64 00:03:18,940 --> 00:03:21,570 struct student, like we did before. 65 00:03:24,320 --> 00:03:28,360 We'll call it S1 66 00:03:28,590 --> 00:03:30,760 By giving the struct a name, 67 00:03:30,760 --> 00:03:33,050 we can now use struct student 68 00:03:33,050 --> 00:03:36,950 in almost the exact same way we would use int. 69 00:03:36,950 --> 00:03:39,580 So we can declare a variable of type struct student, 70 00:03:39,580 --> 00:03:42,360 like 71 00:03:42,360 --> 00:03:49,500 struct student S2. 72 00:03:51,020 --> 00:03:55,130 Like arrays, structs provide a shortcut initialization syntax, 73 00:03:55,130 --> 00:03:58,670 so we can say, struct student S2 74 00:03:58,670 --> 00:04:01,420 equals 75 00:04:01,420 --> 00:04:06,040 left curly brace 3, S2. 76 00:04:09,210 --> 00:04:12,600 Here, S2.age will be 3, 77 00:04:12,600 --> 00:04:15,910 and S2.name will point to S2. 78 00:04:15,910 --> 00:04:19,149 Think of all the things you can do with an int type 79 00:04:19,149 --> 00:04:22,460 and most of them you can do with a struct student type. 80 00:04:22,460 --> 00:04:26,060 We can use a struct student as a type of a function parameter. 81 00:04:26,060 --> 00:04:28,790 We can use struct student inside of a new struct. 82 00:04:28,790 --> 00:04:31,010 We can have a pointer to a struct student. 83 00:04:31,010 --> 00:04:33,540 We can do size of struct student. 84 00:04:33,540 --> 00:04:35,510 Struct student is a type 85 00:04:35,510 --> 00:04:38,030 just like int is a type. 86 00:04:38,030 --> 00:04:40,540 We can also assign S1 to S2 87 00:04:40,540 --> 00:04:43,760 since both are of the same type, so we can do 88 00:04:44,390 --> 00:04:47,540 S1 = S2. 89 00:04:47,540 --> 00:04:50,430 What happens if we do 90 00:04:50,430 --> 00:04:55,300 S1.age = 10? 91 00:04:56,340 --> 00:04:58,880 Does S2 change at all? 92 00:04:58,880 --> 00:05:02,800 Again, think of the structs just as regular integers. 93 00:05:02,800 --> 00:05:05,590 If we assign some int X to some int Y, 94 00:05:05,590 --> 00:05:08,970 like X = Y 95 00:05:08,970 --> 00:05:10,850 and then change X, 96 00:05:10,850 --> 00:05:14,230 as in X++, 97 00:05:14,230 --> 00:05:17,020 does Y change at all? 98 00:05:17,020 --> 00:05:20,980 Y does not change here, and so neither does S2 above. 99 00:05:20,980 --> 00:05:24,120 S2.age is still 3. 100 00:05:24,120 --> 00:05:27,350 But note that when assigning one struct to another, 101 00:05:27,350 --> 00:05:30,300 all of the pointers still point to the same thing, 102 00:05:30,300 --> 00:05:32,260 since they were just copied. 103 00:05:32,260 --> 00:05:34,300 If you don't want the pointers to be shared, 104 00:05:34,300 --> 00:05:36,100 you'll need to manually handle that, 105 00:05:36,100 --> 00:05:39,780 perhaps by malicking one block of memory for one of the pointers to point to 106 00:05:39,780 --> 00:05:42,120 and copying the data over. 107 00:05:42,120 --> 00:05:45,540 It might be annoying to have to write struct student everywhere. 108 00:05:45,540 --> 00:05:48,730 Using a type def, we can do 109 00:05:51,630 --> 00:05:55,850 type def 110 00:05:55,850 --> 00:05:58,830 struct 111 00:05:58,830 --> 00:06:01,270 and we'll call it student. 112 00:06:05,620 --> 00:06:08,360 Now, we can use student everywhere 113 00:06:08,360 --> 00:06:11,090 that we used to use struct student. 114 00:06:11,090 --> 00:06:13,410 This type def's an anonymous struct 115 00:06:13,410 --> 00:06:15,750 and calls it student. 116 00:06:15,750 --> 00:06:18,220 But if we also keep the student identifier 117 00:06:18,220 --> 00:06:22,380 next to the word struct, as in typedef struct student, 118 00:06:27,670 --> 00:06:31,590 we could use both struct student and student interchangeably now. 119 00:06:31,590 --> 00:06:34,060 They don't even have to have the same name. 120 00:06:34,060 --> 00:06:36,710 We could type def struct student to Bob 121 00:06:36,710 --> 00:06:38,950 and then struct student and Bob 122 00:06:38,950 --> 00:06:41,270 would be interchangeable types. 123 00:06:41,270 --> 00:06:44,050 Regardless of the type def, 124 00:06:44,050 --> 00:06:46,750 we need the identifier next to struct 125 00:06:46,750 --> 00:06:48,250 if the definition of the struct 126 00:06:48,250 --> 00:06:50,450 is recursive. 127 00:06:50,450 --> 00:06:52,620 For example, 128 00:06:52,620 --> 00:06:56,140 type def struct node 129 00:06:56,140 --> 00:07:01,200 and it will be defined as an int val 130 00:07:01,200 --> 00:07:05,420 and it will have a pointer that points to another struct node., 131 00:07:05,420 --> 00:07:09,490 as in struct node * next. 132 00:07:09,490 --> 00:07:13,670 And then we'll call it node. 133 00:07:15,490 --> 00:07:18,020 This struct is recursive, 134 00:07:18,020 --> 00:07:21,450 since the definition of struct node contains within it 135 00:07:21,450 --> 00:07:24,200 a pointer to a struct node. 136 00:07:24,200 --> 00:07:27,740 Notice that we have to say struct node * next 137 00:07:27,740 --> 00:07:30,690 inside of the definition of the struct node, 138 00:07:30,690 --> 00:07:33,620 since the type def hasn't finished yet to allow us to simplify this 139 00:07:33,620 --> 00:07:36,210 to just node * next. 140 00:07:36,210 --> 00:07:39,260 You'll learn more about structs similar to this 141 00:07:39,260 --> 00:07:41,750 when dealing with linked lists and trees. 142 00:07:41,750 --> 00:07:44,130 What about structs in a function? 143 00:07:44,130 --> 00:07:46,800 This is also perfectly valid. 144 00:07:46,800 --> 00:07:49,430 We could have 145 00:07:49,430 --> 00:07:53,630 void func 146 00:07:53,630 --> 00:07:55,930 which takes as an argument, 147 00:07:55,930 --> 00:07:59,590 student s 148 00:07:59,590 --> 00:08:02,790 and does something with that student. 149 00:08:05,270 --> 00:08:08,450 And then we can pass it as student struct like so. 150 00:08:08,450 --> 00:08:12,850 Func of S1 from before. 151 00:08:12,850 --> 00:08:15,230 The struct behaves 152 00:08:15,230 --> 00:08:18,460 exactly as an integer would when passed to a function. 153 00:08:18,460 --> 00:08:21,510 Func receives a copy of S1 154 00:08:21,510 --> 00:08:23,690 and so can't modify S1; 155 00:08:23,690 --> 00:08:27,110 rather, only the copy of it that's stored in S. 156 00:08:27,110 --> 00:08:30,010 If you want the function to be able to modify S1, 157 00:08:30,010 --> 00:08:33,000 func will need to take a student * S, 158 00:08:33,000 --> 00:08:36,570 and you'll have to pass S1 by address, like so. 159 00:08:37,549 --> 00:08:41,100 Student * S, func & S1. 160 00:08:41,100 --> 00:08:44,760 There's another reason to pass by address here. 161 00:08:44,760 --> 00:08:48,030 What if our struct contained 100 fields? 162 00:08:48,030 --> 00:08:51,250 Every single time we pass a student to func, 163 00:08:51,250 --> 00:08:55,770 our program needs to copy all of those 100 fields into func's argument S, 164 00:08:55,770 --> 00:08:59,320 even if it never uses the vast majority of them. 165 00:08:59,320 --> 00:09:02,700 So even if func doesn't plan on modifying the student, 166 00:09:02,700 --> 00:09:05,170 if can still be valuable to pass by address. 167 00:09:05,170 --> 00:09:08,990 Okay, what if we want to create a pointer to a struct? 168 00:09:08,990 --> 00:09:11,130 We could do something like 169 00:09:11,130 --> 00:09:17,580 student* S 170 00:09:17,580 --> 00:09:20,980 equals malloc 171 00:09:20,980 --> 00:09:26,600 size of student. 172 00:09:30,450 --> 00:09:33,590 Notice that size of still works here. 173 00:09:33,590 --> 00:09:37,260 So how do we now access the age member 174 00:09:37,260 --> 00:09:39,640 of the block that S points to? 175 00:09:39,640 --> 00:09:42,300 You might first think to do 176 00:09:42,300 --> 00:09:47,970 * S.age = 4, 177 00:09:47,970 --> 00:09:50,220 but this won't quite work. 178 00:09:50,220 --> 00:09:52,940 Since this will really be interpreted as 179 00:09:52,940 --> 00:09:57,740 * S.age in parentheses = 4, 180 00:09:57,740 --> 00:10:00,160 which won't even compile, 181 00:10:00,160 --> 00:10:03,600 since S isn't a struct or rather a pointer to a struct, 182 00:10:03,600 --> 00:10:06,270 and so the dot won't work here. 183 00:10:06,270 --> 00:10:08,860 We could do 184 00:10:08,860 --> 00:10:13,760 (*S).age=4 185 00:10:13,760 --> 00:10:16,790 but the parentheses can get annoying and confusing. 186 00:10:16,790 --> 00:10:19,880 Thankfully, we have a special arrow operator 187 00:10:19,880 --> 00:10:22,350 that looks something like 188 00:10:22,350 --> 00:10:28,860 S->age=4. 189 00:10:28,860 --> 00:10:31,600 These 2 ways of referencing age 190 00:10:31,600 --> 00:10:33,270 are equivalent 191 00:10:33,270 --> 00:10:36,870 and we don't really ever need the arrow operator, 192 00:10:36,870 --> 00:10:39,300 but it makes things look nicer. 193 00:10:39,300 --> 00:10:43,050 Since S is a pointer to some block of memory that contains the struct, 194 00:10:43,050 --> 00:10:47,820 you can think of S>age as follow the pointer arrow 195 00:10:47,820 --> 00:10:50,250 and grab the age member. 196 00:10:50,250 --> 00:10:53,750 So why should we ever use structs? 197 00:10:53,750 --> 00:10:57,560 It's definitely possible to get away with just the primitive integers, 198 00:10:57,560 --> 00:10:59,050 chars, pointers and the like 199 00:10:59,050 --> 00:11:01,550 that we're used to; 200 00:11:01,550 --> 00:11:03,340 instead of S1 and S2 before, 201 00:11:03,340 --> 00:11:06,290 we could have had age1, age2, name1, and name2 202 00:11:06,290 --> 00:11:09,120 all at separate variables. 203 00:11:09,120 --> 00:11:11,390 This is fine with only 2 students, 204 00:11:11,390 --> 00:11:13,310 but what if we had 10 of them? 205 00:11:13,310 --> 00:11:15,540 And what if instead of only 2 fields, 206 00:11:15,540 --> 00:11:17,720 the student struct had 100 fields? 207 00:11:17,720 --> 00:11:21,240 GPA, courses, hair color, gender, and so on. 208 00:11:21,240 --> 00:11:25,790 Instead of just 10 structs, we need 1,000 separate variables. 209 00:11:25,790 --> 00:11:28,360 Also, consider a function 210 00:11:28,360 --> 00:11:32,270 that takes that struct with 100 fields with its only argument 211 00:11:32,270 --> 00:11:34,350 and prints out all fields. 212 00:11:34,350 --> 00:11:36,320 If we didn't use a struct, 213 00:11:36,320 --> 00:11:38,540 every single time we call that function, 214 00:11:38,540 --> 00:11:41,460 we need to pass at all 100 variables, 215 00:11:41,460 --> 00:11:44,430 and if we have 100 variables for student 1, 216 00:11:44,430 --> 00:11:47,020 and 100 variables for student 2, 217 00:11:47,020 --> 00:11:50,540 we need to be sure we don't accidentally pass some variables from student 1 218 00:11:50,540 --> 00:11:52,910 and some variables from student 2. 219 00:11:52,910 --> 00:11:55,710 It's impossible to make that mistake with a struct, 220 00:11:55,710 --> 00:11:59,010 since all 100 variables are contained in a single package. 221 00:11:59,010 --> 00:12:02,050 Just a couple of final notes: 222 00:12:02,050 --> 00:12:04,870 If you've understood everything up to this point, great. 223 00:12:04,870 --> 00:12:07,900 The rest of the video is just for completeness' sake. 224 00:12:07,900 --> 00:12:11,010 Because structs can hold any type of pointer, 225 00:12:11,010 --> 00:12:14,220 they can also hold function pointers. 226 00:12:14,220 --> 00:12:17,040 If you're familiar with object oriented programming, 227 00:12:17,040 --> 00:12:21,790 this provides a way to use structs to program in an object oriented style. 228 00:12:21,790 --> 00:12:24,500 More on function pointers at another time. 229 00:12:24,500 --> 00:12:27,760 Also, sometimes you may have 2 structs 230 00:12:27,760 --> 00:12:30,220 whose definitions depend on one another. 231 00:12:30,220 --> 00:12:32,320 For example, 232 00:12:32,320 --> 00:12:35,470 we could have struct A, 233 00:12:35,470 --> 00:12:38,580 which is defined as 234 00:12:38,580 --> 00:12:41,910 a pointer to a struct B, 235 00:12:41,910 --> 00:12:47,180 struct B * X, 236 00:12:47,180 --> 00:12:50,470 and now we can have a struct B 237 00:12:53,890 --> 00:12:56,280 which is defined as a pointer 238 00:12:56,280 --> 00:12:59,180 to a struct A, 239 00:12:59,180 --> 00:13:03,640 struct A * Y. 240 00:13:07,230 --> 00:13:09,060 But this won't compile, 241 00:13:09,060 --> 00:13:14,110 since struct B doesn't exist at the time that struct A is being compiled. 242 00:13:14,110 --> 00:13:17,600 And if we swap struct A and struct B, 243 00:13:17,600 --> 00:13:20,100 then we'd just be left with the same problem; 244 00:13:20,100 --> 00:13:22,640 this time, with struct A not existing. 245 00:13:22,640 --> 00:13:24,720 To solve this, we can write 246 00:13:24,720 --> 00:13:29,290 struct B; 247 00:13:29,290 --> 00:13:32,460 before the definition of struct A. 248 00:13:32,460 --> 00:13:35,590 This is called a forward declaration. 249 00:13:35,590 --> 00:13:38,590 This just lets the compiler know that 250 00:13:38,590 --> 00:13:42,040 struct B is a valid type that will be fully defined later or elsewhere. 251 00:13:42,040 --> 00:13:45,980 My name is Rob Bowden, and this is CS50. 252 00:13:45,980 --> 00:13:48,980 [CS50.TV]