Learn C Language – Part 3: Character and Pointer

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.

image 01: sample.c

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.

image 02: sample.c

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.

image 03: sample.c

When we step over to line 11, we can confirm that up to ‘s=ABC’ has been printed out on the console.

image 04: the first output result

By stepping over up to line 12, ‘DEF’ has been inserted, and 0 has been moved to the sixth position accordingly.

image 05: sample.c

And here is the last output.

image 06: the last output result

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.

image 07: sample_ex4-2.c

Output result (equal).

image 08: the same values

Output result (not equal).

image 09: not the same values

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.

image 10: sample_ex4-2.c

Three: character -> int

image 10: sample_ex4-3.c

Three: int -> character

image 11: sample_ex4-4.c

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.

image 12: sample_ex4-4.c

The last output result.

image 13: final output

Leave a Reply