Character Pointer in C 

Character Pointer in C

In this article, I am going to discuss Character Pointer in C with Examples. Please read our previous articles, where we discussed Pointer to function in C.

Character Pointer in C:

A pointer may be a special memory location that’s capable of holding the address of another memory cell. So a personality pointer may be a pointer that will point to any location holding character only. Character array is employed to store characters in Contiguous Memory Location. char * and char [] both are wont to access character array, Though functionally both are same, they’re syntactically different. Since the content of any pointer is an address, the size of all kinds of pointers ( character, int, float, double) is 4.

char arr[] = “Hello World”; // array version
char ptr* = “Hello World”;  // pointer version

Example:   
#include<stdio.h>
#include<string.h>
int main ()
{
    char str[10];
    char *ptr;
    printf ("enter a character:\n");
    gets (str);
    puts (str);
    ptr = str;
    printf ("name = %c", *ptr);
}

Output:

Character Pointer in C

Example for better understanding:
#include<stdio.h>
#include<stdlib.h>
int main ()
{
    int n, i;
    char *ptr;
    printf ("Enter number of characters to store: ");
    scanf ("%d", &n);

    ptr = (char *) malloc (n * sizeof (char));
    for (i = 0; i < n; i++)
    {
        printf ("Enter ptr[%d]: ", i);
        /* notice the space preceding %c is
            necessary to read all whitespace in the input buffer
        */
        scanf (" %c", ptr + i);
    }

    printf ("\nPrinting elements of 1-D array: \n\n");
    for (i = 0; i < n; i++)
    {
        printf ("%c ", ptr[i]);
    }

    //signal to operating system program ran fine
    return 0;
}

Output:

Character Pointer in C

In the next article, I am going to discuss Pointer to Constant in C language. Here, in this article, I try to explain Character Pointer in C. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *