1 00:00:05,630 --> 00:00:07,400 In this video, we'll look at the syntax for 2 00:00:07,400 --> 00:00:08,860 declaring pointer variables. 3 00:00:09,650 --> 00:00:12,290 You should not feel very comfortable declaring variables, we've been 4 00:00:12,290 --> 00:00:13,559 doing it throughout the course. 5 00:00:14,090 --> 00:00:17,000 Well, we declare pointer variables in exactly the same way 6 00:00:17,199 --> 00:00:20,009 except that we add the asterisk prior to the variable name. 7 00:00:20,790 --> 00:00:24,660 In this context, the asterisk does not function as a mathematical operator, 8 00:00:24,860 --> 00:00:26,480 it serves to declare the pointer. 9 00:00:27,369 --> 00:00:30,060 The way you read these declarations is right to left. 10 00:00:30,330 --> 00:00:34,270 So the first example int pointer is a pointer to an integer, double 11 00:00:34,270 --> 00:00:36,039 pointer is a pointer to a double. 12 00:00:36,690 --> 00:00:40,440 Notice that in this declaration, I place the asterisk next to the type. 13 00:00:41,070 --> 00:00:43,720 Both styles are equivalent and the compiler doesn't care. 14 00:00:44,349 --> 00:00:47,700 But there's been a long-standing argument among c++ programmers 15 00:00:47,700 --> 00:00:48,640 about which is better. 16 00:00:49,340 --> 00:00:52,110 I always declare my pointers with the asterisk next to the name, 17 00:00:52,110 --> 00:00:54,730 and that's how I'll do it in this course, but I want it to make you 18 00:00:54,730 --> 00:00:56,360 aware of the equivalent styles. 19 00:00:57,470 --> 00:01:01,370 Char pointer is a pointer to a character, and string pointer is 20 00:01:01,370 --> 00:01:03,450 a pointer to a c++ string object. 21 00:01:03,980 --> 00:01:06,869 Just like all variables, if we don't initialize our variables, 22 00:01:07,090 --> 00:01:08,720 they will contain garbage data. 23 00:01:09,339 --> 00:01:11,840 In this case, all the pointer variables declared, 24 00:01:12,000 --> 00:01:13,560 contain garbage data. 25 00:01:14,110 --> 00:01:16,530 So let's see how we can initialize pointer variables. 26 00:01:17,900 --> 00:01:21,810 In c++, it's very important that you always initialize all pointer 27 00:01:21,810 --> 00:01:23,440 variables before you use them. 28 00:01:24,009 --> 00:01:27,720 If you don't initialize a pointer variable, it will have garbage data. 29 00:01:28,570 --> 00:01:31,350 In this case, that garbage data represents an address since 30 00:01:31,350 --> 00:01:32,839 that's what a pointer contains. 31 00:01:33,370 --> 00:01:36,750 So you can think of an uninitialized pointer as pointing anywhere. 32 00:01:37,490 --> 00:01:40,059 So if we use it we could be accessing memory that we have 33 00:01:40,070 --> 00:01:41,600 no business messing around with. 34 00:01:41,920 --> 00:01:43,579 We don't even know what that memory is. 35 00:01:44,099 --> 00:01:46,920 Initializing pointer variables is just like initializing 36 00:01:46,930 --> 00:01:48,100 non-pointer variables. 37 00:01:48,260 --> 00:01:50,380 We can use an initializer list syntax. 38 00:01:51,030 --> 00:01:54,520 In these examples, we're initializing the pointer variables to zero 39 00:01:54,530 --> 00:01:58,269 that's what null pointer represents this means that we're initializing 40 00:01:58,289 --> 00:01:59,929 the pointers to point nowhere. 41 00:02:00,259 --> 00:02:02,410 That's very different from pointing anywhere. 42 00:02:03,179 --> 00:02:06,559 We can also initialize pointers to actually point to a variable, and 43 00:02:06,560 --> 00:02:07,810 we'll do that in the next video. 44 00:02:08,419 --> 00:02:10,870 Let's review what we just talked about since it's very, 45 00:02:10,880 --> 00:02:12,440 very important to understand. 46 00:02:13,410 --> 00:02:15,710 Always initialize all pointer variables. 47 00:02:16,130 --> 00:02:19,670 Uninitialized pointers contain garbage data and point anywhere. 48 00:02:20,599 --> 00:02:24,439 No pointer was introduced in c++11, and it represents 49 00:02:24,480 --> 00:02:27,899 address 0, which means that the pointer is pointing nowhere. 50 00:02:28,350 --> 00:02:31,390 Use null pointer to initialize your pointers unless you initialize 51 00:02:31,390 --> 00:02:34,610 them to a variable to a function, this nulls out the pointer. 52 00:02:35,330 --> 00:02:38,059 I know I've repeated myself several times in this short video, 53 00:02:38,250 --> 00:02:39,450 I'm doing that for a reason. 54 00:02:39,719 --> 00:02:43,030 After a long career programming with many languages that have pointer 55 00:02:43,030 --> 00:02:46,360 variables, I can tell you that one of the most common pointer-related 56 00:02:46,360 --> 00:02:49,720 errors that I've seen in code reviews is uninitialized pointers. 57 00:02:50,190 --> 00:02:52,540 Most of the time this didn't cause a problem, but the 58 00:02:52,540 --> 00:02:54,020 potential for trouble is there. 59 00:02:54,240 --> 00:02:56,019 So always initialize your pointers. 60 00:02:56,389 --> 00:02:59,230 In the next video, we'll see how we can access the address 61 00:02:59,230 --> 00:03:01,970 of the pointer and store addresses in pointer variables.