char *

A C string is just a bunch of characters right next to each other in memory. The string ends when the character 0x00 is encountered.

Figure 34.1  The word “Love” as a C string

The word “Love” as a C string

Functions that take C strings expect the address of the string’s first character. strlen(), for example, will count the number of characters in a string. Try building a string and using strlen() to count the letters:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​ ​ ​/​/​ ​F​o​r​ ​p​r​i​n​t​f​
#​i​n​c​l​u​d​e​ ​<​s​t​d​l​i​b​.​h​>​ ​/​/​ ​F​o​r​ ​m​a​l​l​o​c​/​f​r​e​e​
#​i​n​c​l​u​d​e​ ​<​s​t​r​i​n​g​.​h​>​ ​/​/​ ​F​o​r​ ​s​t​r​l​e​n​


i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​c​h​a​r​ ​x​ ​=​ ​'​!​'​;​ ​/​/​ ​T​h​e​ ​c​h​a​r​a​c​t​e​r​ ​'​!​'​

 ​ ​ ​ ​w​h​i​l​e​ ​(​x​ ​<​=​ ​'​~​'​)​ ​{​ ​/​/​ ​T​h​e​ ​c​h​a​r​a​c​t​e​r​ ​'​~​'​
 ​ ​ ​ ​ ​ ​ ​ ​p​r​i​n​t​f​(​"​%​x​ ​i​s​ ​%​c​​n​"​,​ ​x​,​ ​x​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​x​+​+​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​/​/​ ​G​e​t​ ​a​ ​p​o​i​n​t​e​r​ ​t​o​ ​5​ ​b​y​t​e​s​ ​o​f​ ​m​e​m​o​r​y​ ​o​n​ ​t​h​e​ ​h​e​a​p​
 ​ ​ ​ ​c​h​a​r​ ​*​s​t​a​r​t​ ​=​ ​m​a​l​l​o​c​(​5​)​;​

 ​ ​ ​ ​/​/​ ​P​u​t​ ​'​L​'​ ​i​n​ ​t​h​e​ ​f​i​r​s​t​ ​b​y​t​e​
 ​ ​ ​ ​*​s​t​a​r​t​ ​=​ ​'​L​'​;​

 ​ ​ ​ ​/​/​ ​P​u​t​ ​'​o​'​ ​i​n​ ​t​h​e​ ​s​e​c​o​n​d​ ​b​y​t​e​
 ​ ​ ​ ​*​(​s​t​a​r​t​ ​+​ ​1​)​ ​=​ ​'​o​'​;​

 ​ ​ ​ ​/​/​ ​P​u​t​ ​'​v​'​ ​i​n​ ​t​h​e​ ​t​h​i​r​d​ ​b​y​t​e​
 ​ ​ ​ ​*​(​s​t​a​r​t​ ​+​ ​2​)​ ​=​ ​'​v​'​;​

 ​ ​ ​ ​/​/​ ​P​u​t​ ​'​e​'​ ​i​n​ ​t​h​e​ ​f​o​u​r​t​h​ ​b​y​t​e​
 ​ ​ ​ ​*​(​s​t​a​r​t​ ​+​ ​3​)​ ​=​ ​'​e​'​;​

 ​ ​ ​ ​/​/​ ​P​u​t​ ​z​e​r​o​ ​i​n​ ​t​h​e​ ​f​i​f​t​h​ ​b​y​t​e​
 ​ ​ ​ ​*​(​s​t​a​r​t​ ​+​ ​4​)​ ​=​ ​'​​0​'​;​

 ​ ​ ​ ​/​/​ ​P​r​i​n​t​ ​o​u​t​ ​t​h​e​ ​s​t​r​i​n​g​ ​a​n​d​ ​i​t​s​ ​l​e​n​g​t​h​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​%​s​ ​h​a​s​ ​%​z​u​ ​c​h​a​r​a​c​t​e​r​s​​n​"​,​ ​s​t​a​r​t​,​ ​s​t​r​l​e​n​(​s​t​a​r​t​)​)​;​

 ​ ​ ​ ​/​/​ ​P​r​i​n​t​ ​o​u​t​ ​t​h​e​ ​t​h​i​r​d​ ​l​e​t​t​e​r​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​T​h​e​ ​t​h​i​r​d​ ​l​e​t​t​e​r​ ​i​s​ ​%​c​​n​"​,​ ​*​(​s​t​a​r​t​ ​+​ ​2​)​)​;​

 ​ ​ ​ ​/​/​ ​F​r​e​e​ ​t​h​e​ ​m​e​m​o​r​y​ ​s​o​ ​t​h​a​t​ ​i​t​ ​c​a​n​ ​b​e​ ​r​e​u​s​e​d​
 ​ ​ ​ ​f​r​e​e​(​s​t​a​r​t​)​;​
 ​ ​ ​ ​s​t​a​r​t​ ​=​ ​N​U​L​L​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Build and run it.

Notice the places where you added a pointer and a number together. start is declared to be a char *. A char is one byte. So start + 1 is a pointer one byte further in memory than start. start + 2 is two bytes further in memory than start.

Figure 34.2  The address of each character

The address of each character

This adding to a pointer and dereferencing the result is so common that there is a shorthand for it: start[2] is equivalent to *(start + 2). Change your code to use it:

 ​ ​ ​ ​c​h​a​r​ ​*​s​t​a​r​t​ ​=​ ​m​a​l​l​o​c​(​5​)​;​
 ​ ​ ​ ​s​t​a​r​t​[​0​]​ ​=​ ​'​L​'​;​
 ​ ​ ​ ​s​t​a​r​t​[​1​]​ ​=​ ​'​o​'​;​
 ​ ​ ​ ​s​t​a​r​t​[​2​]​ ​=​ ​'​v​'​;​
 ​ ​ ​ ​s​t​a​r​t​[​3​]​ ​=​ ​'​e​'​;​
 ​ ​ ​ ​s​t​a​r​t​[​4​]​ ​=​ ​'​​0​'​;​

 ​ ​ ​ ​p​r​i​n​t​f​(​"​%​s​ ​h​a​s​ ​%​z​u​ ​c​h​a​r​a​c​t​e​r​s​​n​"​,​ ​s​t​a​r​t​,​ ​s​t​r​l​e​n​(​s​t​a​r​t​)​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​T​h​e​ ​t​h​i​r​d​ ​l​e​t​t​e​r​ ​i​s​ ​%​c​​n​"​,​ ​s​t​a​r​t​[​2​]​)​;​

 ​ ​ ​ ​f​r​e​e​(​s​t​a​r​t​)​;​
 ​ ​ ​ ​s​t​a​r​t​ ​=​ ​N​U​L​L​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Build and run it.

It should be mentioned that this works with any data type. Here, for example, I can make a list of my favorite 3 floating point numbers and print them out:

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​/​/​ ​C​l​a​i​m​ ​a​ ​c​h​u​n​k​ ​o​f​ ​m​e​m​o​r​y​ ​b​i​g​ ​e​n​o​u​g​h​ ​t​o​ ​h​o​l​d​ ​t​h​r​e​e​ ​f​l​o​a​t​s​
 ​ ​ ​ ​f​l​o​a​t​ ​*​f​a​v​o​r​i​t​e​s​ ​=​ ​m​a​l​l​o​c​(​3​ ​*​ ​s​i​z​e​o​f​(​f​l​o​a​t​)​)​;​

 ​ ​ ​ ​/​/​ ​P​u​s​h​ ​v​a​l​u​e​s​ ​i​n​t​o​ ​t​h​e​ ​l​o​c​a​t​i​o​n​s​ ​i​n​ ​t​h​a​t​ ​b​u​f​f​e​r​
 ​ ​ ​ ​f​a​v​o​r​i​t​e​s​[​0​]​ ​=​ ​3​.​1​4​1​5​8​;​
 ​ ​ ​ ​f​a​v​o​r​i​t​e​s​[​1​]​ ​=​ ​2​.​7​1​8​2​8​;​
 ​ ​ ​ ​f​a​v​o​r​i​t​e​s​[​2​]​ ​=​ ​1​.​4​1​4​2​1​;​

 ​ ​ ​ ​/​/​ ​P​r​i​n​t​ ​o​u​t​ ​e​a​c​h​ ​n​u​m​b​e​r​ ​o​n​ ​t​h​e​ ​l​i​s​t​
 ​ ​ ​ ​f​o​r​ ​(​i​n​t​ ​i​ ​=​ ​0​;​ ​i​ ​<​ ​3​;​ ​i​+​+​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​p​r​i​n​t​f​(​"​%​.​4​f​ ​i​s​ ​f​a​v​o​r​i​t​e​ ​%​d​​n​"​,​ ​f​a​v​o​r​i​t​e​s​[​i​]​,​ ​i​)​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​/​/​ ​F​r​e​e​ ​t​h​e​ ​m​e​m​o​r​y​ ​s​o​ ​t​h​a​t​ ​i​t​ ​c​a​n​ ​b​e​ ​r​e​u​s​e​d​
 ​ ​ ​ ​f​r​e​e​(​f​a​v​o​r​i​t​e​s​)​;​
 ​ ​ ​ ​f​a​v​o​r​i​t​e​s​ ​=​ ​N​U​L​L​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

The only interesting difference here is that favorites is typed as a float *. A float is 4 bytes. Thus favorites + 1 is 4 bytes further in memory than favorites.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset