C program to print each character of string using pointer

In this program, we are going to follow up topic of pointer and how do we use it to print character in strings? So let’s first consider that we have taken a string of 6 characters and we want to access each character and print it but not directly rather using pointer. So pointers are used when we want to access some data not directly but indirectly through some other link which points to original data.

For example, I have stored string in variable H and then address of variable H is stored inside J so with use of pointer we can print string by using variable J because variable J points to variable H which holds string so instead of specifying address of variable H we are specifying variable J which holds address of variable H which serves our purpose to print string.

We will get to know more about this when we see program source code.
This program is somewhat similar to previous program we have done for printing each character of string.

Pre-requisites:

Pointers, string, array, input output functions

Approach:

We will take user input as usual from command prompt to keep input dynamic and store the string inside character array.

As we are going to make use of pointer to access each character, declare a character pointer and for doing that suffix pointer variable name by asterisk which will look like char *pointer_var_name. * is called dereference character when used with pointer and set address of character array or string to pointer so that pointer points to first character in array (string is character array).

In this example we are going to use while loop to iterate loop no. of times till each character in array is read or accessed and for this purpose while loop is set to condition that pointer not equals to ‘/0’. Okay so here we need to understand that when we store any string in array then last thing in array is special character ‘/0’ and this indicates the end of that string or character array. So here in our condition this means that when pointer will point to ‘/0’ in character array then exit loop and if not then keep iterating through loop.

Inside while loop body, we will add printf() function to print character(use %c as format specifier) and provide pointer variable *p after format specifier as it holds address of character array that points to first character in array. This printf() function will print the first character array in first loop iteration and before loop enters into second iteration we will increment pointer variable p i.e. p++ so that in next loop iteration the pointer variable with dereference character *p points to second character in array. Okay so let’s discuss why we incremented variable p not *p. So the reason is that when pointer variable p is mentioned then it represent address stored inside p whereas *p represent value at the address stored inside p.

So when we increment address of p then it will increment address that will eventually point to next character. (each character is of 1 byte and address gets incremented by 1 so it points to next character which is at new incremented address)

Step by step approach:

First declare the array of character type, let’s say Char_array[10]. We can put more size based on our input but here only size 10 bytes is given. And then read the string into array. Also declare character pointer and set it to address of character array so that pointer points to first character in array.

Use while loop for iterating through array till character ‘/0’ is not reached. ‘/0’ indicates end of array or last character in array after string and it is present there just to indicate end of array. While loop condition looks like *pt!='\0'

Inside while loop we will add printf() function to print character with format specifier %c and dereferencing pointer (*pt) to access character.
Increment the character pointer p (p++). This increments the address stored inside character pointer by 1 bytes so eventually it points to next character stored inside character array because character pointer holds address of string and when we increment that address it will eventually point to next character in string.

Program:

Output:

Leave a Reply

%d bloggers like this: