1 00:00:00,000 --> 00:00:00,620 2 00:00:00,620 --> 00:00:03,140 >> DAVID J. MALAN: Let's write a program that prompts the user for a string and 3 00:00:03,140 --> 00:00:07,210 then proceed to print that string character for character one per line. 4 00:00:07,210 --> 00:00:10,570 Now in the past, we would have done so probably with square bracket notation, 5 00:00:10,570 --> 00:00:13,680 effectively treating a string is an array of characters. 6 00:00:13,680 --> 00:00:17,200 >> But this time, let's instead treat a string for what it really is, a 7 00:00:17,200 --> 00:00:18,770 pointer or an address. 8 00:00:18,770 --> 00:00:22,420 Specifically, the address of a character, really the address of the 9 00:00:22,420 --> 00:00:25,740 first character, in a sequence of characters that we collectively know 10 00:00:25,740 --> 00:00:26,860 as a string. 11 00:00:26,860 --> 00:00:30,740 >> Let's first declare a string for what it really is, char*. 12 00:00:30,740 --> 00:00:31,770 And we'll call it s. 13 00:00:31,770 --> 00:00:34,670 And then assign it the return value of get string. 14 00:00:34,670 --> 00:00:36,380 >> Let's next do some error checking. 15 00:00:36,380 --> 00:00:42,920 If s is null, let's immediately return so that we don't accidentally 16 00:00:42,920 --> 00:00:45,630 dereference that null pointer. 17 00:00:45,630 --> 00:00:49,750 >> Next, let's iterate over the characters in s as follows. 18 00:00:49,750 --> 00:00:52,390 For int, i gets 0. 19 00:00:52,390 --> 00:00:55,890 n equals the string length of s. 20 00:00:55,890 --> 00:00:58,050 Do this so long as i is less than n. 21 00:00:58,050 --> 00:01:00,690 And on each iteration, increment i. 22 00:01:00,690 --> 00:01:02,710 >> And what do we want to do on each iteration? 23 00:01:02,710 --> 00:01:06,180 Let's now print out on each iteration a single character 24 00:01:06,180 --> 00:01:07,910 followed by a new line. 25 00:01:07,910 --> 00:01:10,010 Well, what character do we want to print? 26 00:01:10,010 --> 00:01:16,850 I propose that we go to the address that equals the sum of s plus i. 27 00:01:16,850 --> 00:01:18,390 >> Now, why that expression? 28 00:01:18,390 --> 00:01:22,130 Well, recall that stored in s is the address of the first character 29 00:01:22,130 --> 00:01:23,490 in our string, s. 30 00:01:23,490 --> 00:01:27,470 Meanwhile, i is being incremented on each iteration so that it starts at 0, 31 00:01:27,470 --> 00:01:29,590 then goes to 1, then goes to 2. 32 00:01:29,590 --> 00:01:33,870 >> So in other words, s plus i effectively represents the address of 33 00:01:33,870 --> 00:01:35,990 the i-th character in s. 34 00:01:35,990 --> 00:01:40,830 So if we go to that address by way of the * operator, we'll be going to the 35 00:01:40,830 --> 00:01:42,650 i-th character in the string. 36 00:01:42,650 --> 00:01:45,700 And that's the value that will be substituted for our placeholder, 37 00:01:45,700 --> 00:01:46,840 percent C. 38 00:01:46,840 --> 00:01:47,840 >> Let's confirm as much. 39 00:01:47,840 --> 00:01:51,720 Let's save, compile, and run this program. 40 00:01:51,720 --> 00:01:55,990 Make pointers, dot slash pointers. 41 00:01:55,990 --> 00:01:58,780 And now I'll give it a string like hello. 42 00:01:58,780 --> 00:01:59,600 Enter. 43 00:01:59,600 --> 00:02:03,770 >> And indeed, I see H-E-L-L-O, with each char on its own line. 44 00:02:03,770 --> 00:02:05,410