Pages

Wednesday, November 24, 2010

Pointers and strings

Strings in C and C++ are defined as "collection of characters terminated by a null character". All the strings must end with the null character("\0") which marks the end of the string. For example:
char text[20];
text[0] = 'H';
text[1] = 'e';
text[2] = 'l':
text[3] = 'l';
text[4] = 'o';
text[5] = '\0';

As you can see the word "hello" has "\0" added to its end. This method of assigning values to individual character is OK for small strings, but for a large string this method is be very time consuming, therefore
we use the following 2 alternate methods.

In the first method  we can write -
char text[20] = {'H', 'e', 'l',  'l', 'o','\0'};

whereas the second method is pretty easy in which we can write like
 char text[20] = "Hello";

Note that the previous examples were using single quotes but the last example is using double quotes. Also we are not appending the null character '\0' ourselves it will be automatically added to the end of  the string "Hello".

No matter which method you choose the compiler will allocate 20 bytes to store the string. Remember that for a string the allocated memory is always contiguous. After the allocation the first 6 bytes are initialized to "Hello\0".

A char can also be pointed to by a char pointer. For this, the pointer has to be declared using the "char" type.

    char c='A';
    char *cp=&a;
    printf("\nC is %c",c); // prints A
    printf("\n*CP is %c",*cp); // prints A


As you can see the pointer(cp) is pointing towards c which contains a value 'A', when printed using the "value at" operator(*) it prints the value of C which is 'A'. Similarly, we can assign the address of a string to a pointer. For example the following code prints a string in a vertical manner.

    char str[]="This is fun";
    for(int i=0;str[i]!='\0';i++) // runs the loop till the end of string is reached.
        printf("\n%c",str[i]);


The same program can be written using a pointer as evident from the following code.
    char str[]="This is fun";
    char *cptr; // A pointer which can point to a char
    for(cptr=str;*cptr!='\0';cptr++)
        printf("\n%c",*cptr);


Before understanding the above code I would like to remind you that an array is actually a pointer pointing to the first element of the collection. This means that it seems that str has the value "This is fun", but actually it is pointing only to  the address of "T" which is the first character of the collection. Assuming that the address of "T" is "100", Here is the internal structure of the string "str" with "This is fun" stored inside it.

Character                    Assumed Address
T                       ->      100
h                       ->       101
i                        ->       102
s                       ->       103
(space)             ->       104
i                        ->       105
s                       ->       106
(space)             ->       107
f                       ->       108
u                      ->       109
n                      ->       110
\0                     ->       111

Inside the loop when we use the statement "cptr=str", we are assigning the value of str(which is 100) to cptr. Therefore both "str" and "cptr" are pointing to the address of "T".  The second part of the loop says that we want to run the loop body till the value at "*cptr" is not equal to the null character. When the loop runs for the first time the value of cptr is 100, but the value at cptr(*cptr) is "T" which definitly is not equal to the null character. Since the condition is true the compiler enters the loop body where the value at "cptr" ("T")is printed. The loop runs again and the value of cptr is incremented by 1(cptr++) . Since it was "100" before it becomes 101 now where the alphabet "h" is stored. "h" is printed and the value is increment again to 102. In this way, the loop reaches to the end of the string where "n" is stored. It advances to the address 111 where the "\0" character is placed. The loop condition becomes false and the loop terminates but only after printing all the characters of the string one by one vertically.

0 comments:

Post a Comment