HowToProgramC : Lesson 23


Back Home Next



Pointer, String and Arrays

We have four basic data types i.e. char, int, float and double. Character strings are arrays of characters. Suppose, there is a word or name like Amir to store in one entity. We cannot store it into a char variable because it can store only one character. For this purpose, a character array is used. We can write it as:

char name [20];

We have declared an array name of 20 characters .It can be initialized as:

name[0] = ‘A’ ;
name[1] = ‘m’ ;
name[2] = ‘i’ ;
name[3] = ‘r’ ;

Each array element is initialized with a single character enclosed in single quote. We cannot use more than one character in single quotes, as it is a syntax error. Is the initialization of the array complete? No, the character strings are always terminated by null character ‘\0’. Therefore, we have to put the null character in the end of the array.

name[4] = ‘\0’ ;

Here we are using two characters in single quotes. But it is a special case. Whenever back slash ( \ ) is used, the compiler considers both the characters as single (also known as escape characters). So ‘\n’ is new line character, ‘\t’ a tab character and ‘\0’ a null character. All of these are considered as single characters. What is the benefit of having this null character at the end of the string? Write a program, do not use the null character in the string and try to print the character array using cout and see what happens? cout uses the null character as the string terminating point. So if cout does not find the null character it will keep on printing. Remember, if we want to store fifteen characters in an array, the array size should be at least sixteen i.e. fifteen for the data and one for the null character. Do we always need to write the null character at the end of the char array by ourselves? Not always, there is a short hand provided in C, i.e. while declaring we can initialize the arrays as:

char name[20] = “Amir”;

When we use double quotes to initialize the character array, the compiler appends null character at the end of the string.“Arrays must be at least one character space larger than the number of printable characters which are to be stored”.

Example Code:

/* This program copies a character array into a given array */
#include <iostream.h>
main( )
{
char strA[80] = "A test string";
char strB[80];
char *ptrA; /* a pointer to type character */
char *ptrB; /* another pointer to type character */
ptrA = strA; /* point ptrA at string A */
ptrB = strB; /* point ptrB at string B */
while(*ptrA != '\0')
{
*ptrB++ = *ptrA++; // copying character by character
}
*ptrB = '\0';
cout << “String in strA = ” << strA << endl; /* show strA on screen */
cout << “String in strB = ” << strB << endl; /* show strB on screen */
}

The output of the program is:
String in strA = A test string
String in strB = A test string

Explanation:

We have declared a char array named strA of size 80 and initialized it with some value say “A test String” using the double quotes. Here we don’t need to put a null character. The compiler will automatically insert it. But while declaring another array strB of the same size, we declare two char pointers *ptrA and *ptrB. The objective of this exercise is to copy one array into another array. We have assigned the starting address of array strA to ptrA and strB to ptrB. Now we have to run a loop to copy all the characters from one array to other. To terminate the loop, we have to know about the actual number of characters or have to use the string termination character. As we know, null character is used to terminate a string, so we are using the condition in 'while loop' as: *ptrA != ‘\0’ , simply checking that whatever ptrA is pointing to is not equal to ‘\0’. Look at the statement *ptrB++ = *ptrA++. What has happened in this statement? First of all, whatever ptrA is pointing to will be assigned to the location where ptrB is pointing to. When the loop starts, these pointers are pointing to the start of the array. So the first character of strA will be copied to the first character of strB. Afterwards, the pointers will be incremented, not the values they are pointing to. Therefore, ptrA is pointing to the 2nd element of the array strA and ptrB is pointing to the 2nd element of the array strB. In the 2nd repetition, the loop condition will be tested. If ptrA is not pointing to a null character the assignment for the 2nd element of the array takes place and so on till the null character is reached. So all the characters of array strA are copied to array strB. Is this program complete? No, the array strB is not containing the null character at the end of the string. Therefore, we have explicitly assigned the null character to strB. Do we need to increment the array pointer? No, simply due to the fact that in the assignment statement ( *ptrA++ = *ptrB++;), the pointers are incremented after the assignment. This program now successfully copies one string to other using only pointers.

To further understand pointers, let's consider the following statement.

char myName[] = "Full Name";

This statement creates a 'char' type array and populates it with a string. Remember the character strings are null ( '\0' ) terminated. We can achieve the same thing with the use of pointer as under:

char * myNamePtr = "Full Name";

When we create an array, the array name, 'myName' in this case, is a constant pointer. The starting address of the memory allocated to string "FullName" becomes the contents of the array name 'myName' and the array name 'myName' can not be assigned any other value. In other words, the location to which array names points to can not be changed. In the second statement, the 'myNamePtr' is a pointer to a string "FullName", which can always be changed to point to some other string.



Back Home Next


Google




learntoknow@yahoo.com
© All rights Reserved.