String Pointers in C Programming

Character datatypes are used to hold only 1 byte of character. It holds only one character in a variable. But we need to have more features from this character datatype as we have words / sentences to  be used in the programs. In such cases we create array of characters to hold the word / string values and add null character ‘\0’ to indicate the end of the string.

Suppose we have a string ‘C Pointer to be stored in a variable in the program. Then we create an array of character to hold this value. Hence we declare and initialize it as follows:

char chrString [] = {‘C’,’ ’,’P ’,’o ’,’i ’,’n ’,’t ’,’e ’,’r ’,’s’,’\0’};

 OR

char chrString [] = “C Pointers”; // Double quotes are used instead of above representation

 

We can observe from above declaration and diagram that it is also an array like any other array that we have already discussed. Hence we can have pointers to these character arrays too like other array pointers. When the pointers are used for character array or strings, then it is called as string pointers. It works similar to any other array pointers. When we increment or decrement string pointers, it increments or decrements the address by 1 byte.

Let us try to understand string pointer using a program. Consider the program below. chrString and chrNewStr are the two strings. First string is initialized to ‘C Pointers’ where as second string is not. String Pointers chrPtr and chrNewPtr are initialized to chrString and chrNewString respectively.  Initially chrPtr will point to the first character of the string chrString. In the while loop below we can see that each character pointed by the chrPtr (‘C Pointers’) is compared with NULL and loop is executed till the end – till null ‘\0’ is encountered. Inside the loop, each character of the pointer is displayed and the pointer is incremented by 1 to point to next character. In the next step, the each character is copied to another pointer chrNewPtr. At the same time, both the pointers are incremented to point to next character. Here we can see increment operator is used along with ‘*’, i.e.;*chrPtr++. The compiler will split it as *chrPtr and chrPtr++; which means it will first assign the value pointed by current address in chrPtr and then it will be incremented to point to next address. Hence it copies character by character to another pointer and is not overwritten nor is same value copied. When chrPtr reaches its end, it encounters ‘\0’. Hence the while loop terminates without copying ‘\0’ to new pointer. Hence we explicitly terminate the new string by assigning ‘\0’ at the end. Thus when we print the value of new string, chrNewStr, it gets the value copied to chrNewPtr pointer. Hence it displays the copied value.

#include <stdio.h>
int main()
{
	char chrString[] = "C Pointers";
	char chrNewStr[20];
	char   *chrPtr;
	char   *chrNewPtr;

	chrPtr = chrString; // Pointer is assigned to point at the beginning of character array chrString
	chrNewPtr = chrNewStr; //Assign pointer chrNewPtr to point to string chrNewStr

	printf("String value pointed by pointer is :");
		while (*chrPtr!= '\0') {
			printf("%c", *chrPtr); // displays the value pointed by pointer one by one
			*chrNewPtr++ = *chrPtr++; // copies character by character to new pointer
		}
	*chrNewPtr = '\0';
	printf("New copied string pointer is :");
		puts(chrNewStr);
	return 0;
}

 


Next > < Prev
Scroll to Top