Tutorial on C Pointers – string handling using pointers

Free C tutorials >> String Handling using Pointers in C >> C language Courses >> Java Courses


The concept of pointers in C in string handling described here using practical examples and with source code and compilation screen shots. Also I have discussed the common errors in pointers to handle strings.

Here a is a character variable and a character ‘C’ is assigned to a. A pointer variable ptr holds the address of a variable (&a).

In the next program I have assigned a whole string to another pointer variable s. So keep in mind that a pointer variable to a character can point to a single character as well as can point to an array of characters i.e a string. In the second case it points to the base address of the string (character array).

The following program describes this very clearly and after the program I have shown the picture of the compilation and output so you can see how it looks after compilation I have used the compiler Dev C++ to run & compile the program.

#include
int main()
{
	char *ptr, a;
	a = 'C';
	ptr = &a;
	printf("\r\n%c %c", a, *ptr);
}
character printing in C using pointer notation
character printing in C using pointer notation

Assign a string to a pointer variable (char *ptr)

Description : Here ptr is a pointer variable which points to a character, and cannot point to an integer (int) or float normally. Only with type casting it can be done, but that is another story.

#include
int main()
{
        char *ptr, a, *s = "Hello World";
	a = 'C';
	ptr = &a;
	printf("\r\n%c %c %s", a, *ptr, s);
}
Character and String printing in C using pointers
Character and String printing in C using pointers

Here I have described few areas using pointers in C in this program given below and the compilation screen shot shows the error in DEV C++ compiler in the following picture.

Common errors in pointers – when re-assign a string to previously defined pointer

#include
int main()
{
	char *p = "Hello World";
	printf("%s", p);
	*p = "Bikram Chouhdhury";
	printf("%s", p);
}
Pointer in C language , String handling
Pointer in C language , String handling

I have taken a pointer variable (p), which points to a character i.e char *p now pointing to a string “Hello World”. A string is basically a character array with the \0 at the end of the array. The character pointer p points to starting location of this character array / string “Hello World” placed in the memory locations, p only points to the starting location of this string i.e points to character ‘H’.

When you printing the string you are mentioning %s inside the printf() function and passing the starting address p as the parameter of printf statement to print the whole string.

But when I am re-assigning another string to the same pointer variable by the statement *p = “Bikram Choudhury” , it is not allowed.

So this pointer re-assignment is not allowed in C , just check the image given above. I am using Dev C++ compiler and it shows you cannot re-assign a different string to the same pointer variable, which has been already assigned  a character array or string, this is forbidden in C.
————————————————————————————-

#include
int main()
{
	char *p = "Hello World", *ptr;
	ptr=p;
	printf("\r\n%s", p);
	printf("\r\n%s", ptr);
}

In the above program I have assigned a string, that is a character array “Hello World” to a pointer variable p. I have taken another pointer variable *ptr
Later I have assigned pointer p to the pointer variable ptr, then I have printed the string using both pointer variables p & ptr using the printf() statement, so you will see the output Hello World in both cases in the following picture in which I have shown the output.

Assign pointer to another pointer variable in C
Assign pointer to another pointer variable in C

————————————————————————————-

#include 
#define ARRSIZE 100
int main()
{
	char *oneDimensionCharArray = "Hello World";	
	printf("%s\r\n",oneDimensionCharArray);
	puts(oneDimensionCharArray);
}

In the above program I have taken character pointer *oneDimensionCharArray that actually points to the string “Hello World”, that is the base address of string, points to ‘H’, the starting address of the array of characters. Then I have printed using the printf() and puts() function.


C tutor in Kolkata, C private tutor, Online C language tutor