Character Pointers in C: Understanding Their Role and Usage

Discover what character pointers are in C programming, their significance in string manipulation, and how to effectively use them. Learn about pointers to character arrays and their importance in function arguments for string processing.


Character Pointers and Functions in C

What is a Character Pointer in C?

A character pointer is a variable that holds the address of a character type or the address of the first character in a character array (string). Character pointers are particularly useful for manipulating strings, as C does not have a dedicated string data type. Instead, strings are represented as arrays of type char. This means that a pointer to a char array can represent a string, and you can pass it as an argument to functions for string processing.

Declaring a Character Pointer

To declare a character pointer, you can use the following syntax:

Syntax
char *pointer_name;

Initializing a Character Pointer

After declaring a character pointer, you need to initialize it with the address of a character variable. If you have a character array, you can initialize the pointer by using the name of the array or by providing the address of its first element.

Character Pointer of Character

The syntax to initialize a character pointer for a single character is:

Syntax
char *pointer_name = &char_variable;

Character Pointer of Character Array

The syntax to initialize a character pointer for a character array (string) is:

Syntax
char *pointer_name = char_array;
Syntax
char *pointer_name = &char_array[0];

Character Pointer Example

The following example illustrates how to use character pointers with both a single character and a character array:

Code Example

#include 

int main() {
  // Declare two variables
  char x = 'Q';
  char arr[] = "Learning C";

  // Declaring character pointers
  char *ptr_x = &x;
  char *ptr_arr = arr;

  // Printing values
  printf("Value of x : %c\n", *ptr_x);
  printf("Value of arr: %s\n", ptr_arr);
  
  return 0;
}
        
Output
Value of x : Q
Value of arr: Learning C
        

Understanding Character Pointer

A string in C is declared as an array like this:

Code Example
char arr[] = "World";

This array is a NULL-terminated sequence of characters, meaning the last character is the NULL character (\0).

To declare a pointer of char type and assign it the address of the first character, you can do:

Code Example
char *ptr = &arr[0];

Note that the name of the array itself serves as the address of the 0th element:

Code Example
char *ptr = arr;

You can also declare a string using a pointer without an array variable:

Code Example
char *ptr = "World";

By incrementing the pointer, you can traverse the string:

Code Example
while(*ptr != '\0') {
   printf("%c", *ptr);
   ptr++;
}

Accessing Character Array

You can print a character array using the %s format specifier by using the name of the character pointer. To access each character, use an asterisk (*) before the pointer name and then increment it.

Example

Here's a complete program to demonstrate this:

Code Example

#include 

int main() {
   char arr[] = "Character Pointers and Functions in C";
   char *ptr = arr;

   while(*ptr != '\0') {
      printf("%c", *ptr);
      ptr++;
   }
}
        
Output
Character Pointers and Functions in C
        

Example with printf

You can also pass the pointer to printf() using the %s format to print the string:

Code Example

#include 

int main() {
   char arr[] = "Character Pointers and Functions in C";
   char *ptr = arr;

   printf("%s", ptr);
}
        
Output
Character Pointers and Functions in C
        

Character Pointer Functions

The string.h header file defines several library functions for string processing, such as finding the length of a string, copying a string, and comparing two strings. These functions typically use char pointer arguments.

The strlen() Function

The strlen() function returns the length of a string, which is the number of characters in it. Its prototype is:

Syntax
int strlen(char *);

Example 1

The following code demonstrates how to use strlen() to print the length of a string:

Code Example

#include 
#include 

int main() {
   char *ptr = "Welcome";

   printf("Given string: %s \n", ptr);
   printf("Length of the string: %d", strlen(ptr));

   return 0;
}
        
Output
Given string: Welcome 
Length of the string: 7
        

Example 2

The following code demonstrates how to compute the string length using a user-defined function called str_len():

Code Example

#include 
#include 

int str_len(char *);

int main() {
   char *ptr = "Exploring C";
   int length = str_len(ptr);
   printf("Given string: %s \n", ptr);
   printf("Length of the string: %d", length);
   
   return 0;
}

int str_len(char *ptr) {
   int i = 0;
   while(*ptr != '\0') {
      i++;
      ptr++;
   }
   return i;
}
        
Output
Given string: Exploring C 
Length of the string: 15
        

The strcpy() Function

Instead of using the assignment operator (=) to assign a string to a char pointer, you need to use the strcpy() function with the following prototype:

Syntax
char * strcpy(char * dest, char * source);

Example 1

The following example shows how to use the strcpy() function:

Code Example

#include 
#include 

int main() {
   char *ptr = "Good Morning";
   char *ptr1;

   strcpy(ptr1, ptr);
   printf("%s", ptr1);

   return 0;
}
        
Output
Good Morning
        

Example 2

Internally, the strcpy() function can be implemented in a user-defined function called str_cpy():

Code Example

#include 
#include 

void str_cpy(char *d, char *s);

int main() {
   char *ptr = "Using the strcpy() Function";
   char *ptr1;

   str_cpy(ptr1, ptr);
   printf("%s", ptr1);
   return 0;
}

void str_cpy(char *d, char *s) {
   int i;
   for(i = 0; s[i] != '\0'; i++)
      d[i] = s[i];

   d[i] = '\0';
}
        
Output
Using the strcpy() Function
        

The strcmp() Function

To compare two strings, you cannot use the usual comparison operators (<, >, <=, >=, ==, !=). Instead, you must use the strcmp() function from the string.h header file. Its prototype is:

Syntax
int strcmp(char *str1, char *str2);

The strcmp() function can return three possible values:

  • Returns 0 if both strings are identical.
  • Returns a positive integer if the first non-matching character in str1 has a greater ASCII value than in str2, implying str1 appears after str2 in alphabetical order.
  • Returns a negative integer if the first non-matching character in str1 has a lesser ASCII value than in str2, implying str1 appears before str2 in alphabetical order.

Example 1

The following example demonstrates how to use the strcmp() function in a C program:

Code Example

#include 
#include 

int main() {
   char *s1 = "HELLO";
   char *s2 = "WORLD";

   int ret = strcmp(s1, s2);

   if (ret == 0)
      printf("Both strings are identical\n");
   else if (ret > 0)
      printf("The first string appears after the second string \n");
   else
      printf("The first string appears before the second string \n");

   return 0;
}
        
Output
The first string appears before the second string 
        

Example 2

You can achieve similar results using a user-defined function called str_cmp():

Code Example

#include 
#include 

int str_cmp(char *str1, char *str2);

int main() {
   char *s1 = "Learning C is Fun";
   char *s2 = "Learning C is Fun";

   int ret = str_cmp(s1, s2);

   if (ret == 0)
      printf("Both strings are identical\n");
   else if (ret > 0)
      printf("The first string appears after the second string\n");
   else
      printf("The first string appears before the second string\n");

   return 0;
}

int str_cmp(char *str1, char *str2) {
   while (*str1 != '\0' && *str2 != '\0') {
      if (*str1 != *str2) {
         return *str1 - *str2;
      }
      str1++;
      str2++;
   }
   return 0;
}
        
Output
Both strings are identical