
Character and Pointer.
The thrid installment in C’s basics. This time, let’s take a look at the relationship between strings/characters and pointers. This is also an integral part of better understanding the C language and its relationship with OSes.
It’s been a little long overdue since the second installment of this series. In our third installment, we’ll uncover the relationship between strings/characters and pointers.
One: Character’s functions
A string is an array of characters, did you know that? So, we can use a series of characters as pointers.
As always, by inserting the breakpoint, let’s see how the program works.
In image02, we’ve inserted a breakpoint in line 8. At this point, no value is set in s, and len has just been initialized.
We’ve come to line 9. At this point, the string ‘ABC’ is inserted in s, and by expanding the variable, we can see ten characters are in it. The third 0 (0’\0′) means that it is the end of the string.
When we step over to line 11, we can confirm that up to ‘s=ABC’ has been printed out on the console.
By stepping over up to line 12, ‘DEF’ has been inserted, and 0 has been moved to the sixth position accordingly.
And here is the last output.
Two: Character’s functions
In this program, we’ll check if the values of s1 and s2 are the same.
*In line 5, we’ve got a little extra size of 256.
Output result (equal).
Output result (not equal).
Arrays are handled just like other pointers.
In line 8, the variable is written in the form of a usual array-type variable: s1. ANd just for a comparison, we have extra-added lines 6 and line 9. Usual int-type variables are written in the form of &a, and that’s the difference in syntax with array-type variables.
And keep in mind that, strcmp function is responsible for comparison in this program.
Three: character -> int
Three: int -> character
Inserting the breakpoint at line 7, we can see that the number 100 is substituted in s1, and 1,0,0 are substituted to s1’s 0,1,2 respectively. And finally, the newline code is inserted in 3.
The last output result.