Pointer To String In C Programming

Share Your Love

Pointer to a string acts similar to that of a pointer to array, here the pointer variable holds the address of the first-string variable and hence the required operation is executed.

Syntax:

char ch[5]=”ABCD”;
char *ptr=ch; 
string pointer
String Pointer

The above description depicts the pointer to a string where the first address value 1000 is stored in the pointer variable.

The next address is incremented by 1 as each character of a string holds a value of 1 memory space. This 1000 or the first address is used to access the string.

Let’s see an example how pointer is used to access an string:

#include<stdio.h>
int main()
{
	char ch[10]="POINTER";
	char *ptr=ch;
	while(*ptr!='\0')
	{
		printf("%c",*ptr);
		ptr++;		
	}
	return 0;
}

Here in the above program the address of the first-string variable is stored in the pointer variable as the string is initialised in it. 

The pointer keeps on incrementing with 1 as each character holds a value of 1 memory space until it reaches to the end or NULL and keeps on printing the string by invoking the pointer, then the loop is terminated.

Output:

RuBpPr7w3hUdtbUCKjUFYsJ K4SnSRIhQpu2vzOtMsBt ht0YfeNx3fh40RHuxe hcfI9tSsOsl EtVURyvf ZkTCRGXiYamY10Nr wDReXFPTytYOQL2 5hp41G99NzA6ROgA

Hence, we have reached the end of the topic. Hope we have cleared all your doubts.

Please comment and share this article if you find helpful and WhatsApp me to improve this article.

Share Your Love
Avatar photo
Soham Malakar

Hello my name is Soham Malakar am currently pursuing BCA

Coding is one of my favorite hobbies.
I love to code and an enthusiast to learn different types of programming languages.
I love to get intoxicated in the world of technology and video games.
A quick learner possessing better results.

Articles: 40

Newsletter Updates

Enter your email address below to subscribe to our newsletter