Character Pointer in C Language:
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 the 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:
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; }