C tutorial: Pointer & String in C programming

Programming languages or concepts

 The study of strings is useful to further tie in the relationship between pointers and arrays.

It also makes it easy to illustrate how some of the standard C string functions can be

implemented. Finally it illustrates how and when pointers can and should be passed to

functions.

In C, strings are arrays of characters. This is not necessarily true in other languages. In

BASIC, Pascal, Fortran and various other languages, a string has its own data type. But in

C it does not. In C a string is an array of characters terminated with a binary zero

character (written as '\0'). To start off our discussion we will write some code which,

while preferred for illustrative purposes, you would probably never write in an actual

program. Consider, for example:

char my_string[40];

my_string[0] = 'T';

my_string[1] = 'e';

my_string[2] = 'd':

my_string[3] = '\0';

While one would never build a string like this, the end result is a string in that it is an

array of characters terminated with a nul character. By definition, in C, a string is an

array of characters terminated with the nul character. Be aware that "nul" is not the same

as "NULL". The nul refers to a zero as defined by the escape sequence '\0'. That is it

occupies one byte of memory. NULL, on the other hand, is the name of the macro used to

initialize null pointers. NULL is #defined in a header file in your C compiler, nul may not

be #defined at all.

Since writing the above code would be very time consuming, C permits two alternate

ways of achieving the same thing. First, one might write:

char my_string[40] = {'T', 'e', 'd', '\0',};

But this also takes more typing than is convenient. So, C permits:

char my_string[40] = "Ted";

When the double quotes are used, instead of the single quotes as was done in the previous

examples, the nul character ( '\0') is automatically appended to the end of the string.


The compiler sets aside an contiguous

block of memory 40 bytes long to hold characters and initialized it such that the first 4

characters are Ted\0.

close