twitter
    Find out what I'm doing, Follow Me :)

A String Variable Is Actually A Pointer

In theprogramming language C, a string variable is defined to be simply a pointer to the beginning of a string. This will take some explaining. Refer to the example program on your monitor. You will notice that first we assign a string constant to the string variable named "strg" so we will have some data to work with. Next, we assign the value of the first element to the variable "one", a simple "char" variable. Next, since the string name is a pointer by definition of the C language, we can assign the same value to "two" by using the star and the string name.

The result if the two assignments are such that "one" now has the same value as "two", and both contain the character "T", the first character in the string. Note that it would be incorrect to write the ninth line as "two = *strg[0];" because the star takes the place of the square brackets.

For all practical purposes, "strg" is a pointer. It does, however, have one restriction that a true pointer does not have. It cannot be changed like a variable, but must always contain the initial value and therefore always points to its string. It could be thought of as a pointer constant, and in some applications you may desire a pointer that cannot be corrupted in any way. Even though it cannot be changed, it can be used to refer to other values than the one it is defined to point to, as we will see in the next section of the program.Moving ahead to line 12, the variable "one" is assigned the value of the ninth variable (since the indexing starts at zero) and "two" is assigned the same value because we are allowed to index a pointer to get to values farther ahead in the string. Both variables now contain the character "a".

The C programming language takes care of indexing for us automatically by adjusting the indexing for the type of variable the pointer is pointing to. In this case, the index of 8 is simply added to the pointer value variable before looking up the desired result because a "char" type variable is one byte long. If we were using a pointer to an "int" type variable, the index would be doubled and added to the pointer before looking up the value because an "int" type variable uses two bytes per value stored. When we get to the chapter on structures, we will see that a variable can have many, even into the hundreds or thousands, of characters per variable, but the indexing will b  handled automatically for us by the system.

Since "there" is already a pointer, it can be assigned the value of the eleventh element of "strg" by the statement in line 16 of the program. Remember that since "there" is a true pointer, it can be assigned any value as long as that value represents a "char" type of address. It should be clear that the pointers must be "typed" in order to allow the pointer arithmetic described in the last paragraph to be done properly. The third and fourth outputs will be the same, namely the letter "c".