1 00:00:05,520 --> 00:00:09,510 In this short video, we'll see how the const qualifier affects pointers. 2 00:00:10,270 --> 00:00:12,700 We've already seen that the use of const with function 3 00:00:12,700 --> 00:00:15,690 reference parameters essentially makes them read-only. 4 00:00:16,309 --> 00:00:19,250 In c++, we can qualify a pointer in several ways. 5 00:00:19,480 --> 00:00:21,670 First, we don't have to use const at all. 6 00:00:21,860 --> 00:00:25,580 In this case, we have a pointer just as we've been using up to this point. 7 00:00:25,770 --> 00:00:28,789 We can change the data the pointer is pointing to, and we 8 00:00:28,790 --> 00:00:31,520 can change the pointer itself and make it point somewhere else. 9 00:00:32,280 --> 00:00:35,440 The const qualifier gives us more fine-grained control of 10 00:00:35,440 --> 00:00:36,810 what we allow to be changed. 11 00:00:37,129 --> 00:00:40,640 We can have pointers to constants, constant pointers and 12 00:00:40,640 --> 00:00:42,260 constant pointers to constants. 13 00:00:42,710 --> 00:00:44,750 These will be very useful when we pass pointers to 14 00:00:44,750 --> 00:00:46,030 functions in the next video. 15 00:00:46,240 --> 00:00:47,750 Let's take a look at each one of them. 16 00:00:49,610 --> 00:00:53,729 In the case of pointers to constants, the data pointed to by the pointer 17 00:00:53,730 --> 00:00:55,489 is constant and can't be changed. 18 00:00:55,710 --> 00:00:58,780 However, the pointer itself can change and point elsewhere. 19 00:00:59,130 --> 00:01:00,600 Let's take a look at this example. 20 00:01:01,060 --> 00:01:03,179 We have two integers declared and initialized. 21 00:01:03,389 --> 00:01:05,099 These are high score and low score. 22 00:01:05,530 --> 00:01:08,860 But this time when we declare the pointer, we use the const qualifier 23 00:01:08,870 --> 00:01:10,589 before the type that we point to. 24 00:01:11,160 --> 00:01:15,510 So score pointer is a pointer to an integer constant, and we 25 00:01:15,510 --> 00:01:17,600 initialize it to point to high score. 26 00:01:18,310 --> 00:01:20,789 If we attempt to change the value of the point to two 27 00:01:20,789 --> 00:01:23,910 data, in this case, high score, we'll get a compiler error. 28 00:01:24,740 --> 00:01:28,070 However, we're free to assign another address to the pointer and have 29 00:01:28,070 --> 00:01:30,890 it point somewhere else as we do in the last assignment statement. 30 00:01:33,400 --> 00:01:37,160 In the case of constant pointers, it's the pointer itself that's constant. 31 00:01:37,450 --> 00:01:41,140 So the data pointed to by the pointer can be changed via the pointer. 32 00:01:41,300 --> 00:01:44,890 However, the pointer itself cannot be changed and can't 33 00:01:44,900 --> 00:01:46,040 point to anything else. 34 00:01:46,619 --> 00:01:49,470 Notice that we still use the const qualifier, however, we 35 00:01:49,490 --> 00:01:52,490 use it before the asterisk in the pointer declaration. 36 00:01:52,930 --> 00:01:56,090 So we're declaring score pointer as a const pointer to integer. 37 00:01:56,980 --> 00:01:58,550 All right, let's look at the last case. 38 00:01:59,710 --> 00:02:02,490 In this case, it's a constant pointer to constants. 39 00:02:03,190 --> 00:02:06,649 We need to use the const qualifier twice in the pointer declaration. 40 00:02:07,520 --> 00:02:11,429 So the data pointed to by the pointer is constant and can't 41 00:02:11,430 --> 00:02:15,250 change, and the pointer itself is also constant and can't change. 42 00:02:15,620 --> 00:02:19,210 Notice the syntax, score pointer is a constant pointer 43 00:02:19,230 --> 00:02:20,540 to an integer constant. 44 00:02:21,230 --> 00:02:24,509 In this case, if we try to modify either the data or the 45 00:02:24,520 --> 00:02:26,530 pointer, we get compiler errors. 46 00:02:27,390 --> 00:02:30,450 The const qualifier is very useful and it's often used when 47 00:02:30,450 --> 00:02:31,990 passing pointers to functions. 48 00:02:32,179 --> 00:02:33,640 We'll see that in the next video.