Back to C Programming
2026-07-146 min read

C - Character Pointers and Functions

Learn C - Character Pointers and Functions step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on C - Character Pointers and Functions! In this lesson, we'll delve into the fundamental aspects of character pointers and functions in C programming. These concepts are essential for understanding string manipulation, dynamic memory allocation, and function arguments. They play a crucial role in system programming, embedded systems, competitive coding, debugging, optimizing code performance, and fixing bugs during software development.

Prerequisites

Before we dive into character pointers and functions, it's important to have a strong foundation in C basics:

  1. Familiarize yourself with variables, data types, operators, control structures, and loops.
  2. Gain proficiency in pointers in C, including pointer declarations, dereferencing, and arithmetic operations.
  3. Understand arrays and their properties, including multi-dimensional arrays.
  4. Be well-versed in basic I/O functions like printf() and scanf().

Core Concept

Character Pointers

Character pointers are variables that store the memory address of another variable (of type char). In C, we use the asterisk (*) to declare a character pointer:

char *pointer; // Declaring a character pointer

To assign a value to a character pointer, you can use the & operator to get the memory address of another char variable:

char c = 'A';
char *pointer = &c;

Now, pointer holds the memory address of the variable c. To access the value stored in c through the pointer, we use the dereference operator (*):

printf("%c", *pointer); // Outputs: A

Functions with Character Pointers

Functions can work with character pointers. For example, consider a simple function that takes a character pointer as an argument and increments the value it points to:

void incrementChar(char *ptr) {
(*ptr)++; // Incrementing the character pointed by ptr
}

int main() {
char c = 'A';
char *pointer = &c;

printf("%c\n", c); // Outputs: A
incrementChar(pointer);
printf("%c\n", c); // Outputs: B

return 0;
}

Character Arrays and Pointers

Character arrays are a sequence of characters terminated by the null character (\0). When we declare an array of characters, it's equivalent to declaring a pointer to the first element of the array:

char arr[5] = {'A', 'B', 'C', 'D', '\0'}; // Declaring a character array and initializing it
char *arrPtr = arr; // Assigning the array to a pointer

Now, arrPtr points to the first element of the array ('A'), and we can access other elements by incrementing the pointer:

printf("%c\n", *(arrPtr + 1)); // Outputs: B
printf("%c\n", *(arrPtr + 2)); // Outputs: C

Dynamic Memory Allocation with Pointers

Dynamic memory allocation allows us to reserve space at runtime for character arrays. This is done using functions like malloc(), calloc(), and realloc(). Here's an example of allocating memory for a character array:

char *arr = (char *) malloc(size); // Allocating memory for an array of size elements

String Manipulation with Pointers

String manipulation functions like strcpy(), strcmp(), and strlen() work by using character pointers to traverse strings. These functions are essential for handling string operations in C.

Worked Example

Let's create a simple function that concatenates two character arrays using pointers:

void concatArrays(char *arr1, char *arr2, char *result) {
int i = 0;
while (arr1[i] != '\0') {
result[i] = arr1[i];
i++;
}
int j = 0;
while (arr2[j] != '\0') {
result[i + j] = arr2[j];
j++;
i++;
}
result[i + j] = '\0'; // Adding the null character to mark the end of the string
}

int main() {
char str1[10] = "Hello";
char str2[5] = "World";
char result[15];

concatArrays(str1, str2, result);
printf("%s\n", result); // Outputs: HelloWorld

return 0;
}

Common Mistakes

  1. Forgetting to initialize character pointers and arrays: Always make sure your pointers and arrays are initialized before use.
  2. Overrunning array bounds: Be careful not to access elements beyond the declared size of an array, as this can lead to undefined behavior.
  3. Misunderstanding pointer arithmetic: Remember that incrementing a pointer moves it to the next memory location of its data type.
  4. Forgetting to add the null character when concatenating strings: Don't forget to add the null character at the end of your concatenated string.
  5. Using incorrect function prototypes: Make sure you declare functions with the correct number and types of arguments, including pointers when necessary.
  6. Not freeing dynamically allocated memory: Always remember to free dynamically allocated memory using free() once it's no longer needed.
  7. Assigning incompatible data types to character pointers: Be cautious about assigning values of different data types (e.g., integers) to character pointers, as this can lead to undefined behavior or type conversions.
  8. Not handling NULL pointers properly: Always check for NULL pointers before dereferencing them to avoid segmentation faults.
  9. Ignoring the difference between strings and arrays of characters: Remember that a null-terminated string is not the same as an array of characters, as the former contains an additional null character (\0) at the end.

Practice Questions

  1. Write a function that reverses a given character array using pointers.
  2. Implement a function that finds the length of a character array without using built-in functions like strlen().
  3. Create a function that checks whether two given character arrays are anagrams (i.e., they contain the same characters).
  4. Write a function that compares two strings lexicographically using pointers and returns the lexicographically smaller string.
  5. Implement a simple Caesar cipher encryption/decryption function using character pointers and arrays.
  6. Write a recursive function to find the longest common substring between two character arrays using pointers.
  7. Create a function that sorts an array of strings using quicksort with a pointer-based approach.
  8. Implement a function that counts the number of occurrences of a specific character in a given string using pointers.
  9. Write a function that removes duplicate characters from a character array using pointers and without using any built-in functions.
  10. Create a function that finds the first non-repeating character in a given string using pointers.

FAQ

Q: What happens if I assign an integer to a character pointer?

A: Assigning an integer to a character pointer leads to undefined behavior, as the memory address of an integer is not compatible with that of a char.

Q: How do I compare two strings in C using pointers?

A: You can compare two strings by comparing their characters one by one until you find a difference or reach the null character (\0).

Q: Can I use a single pointer to access elements of multi-dimensional arrays?

A: Yes, you can use a single pointer to access elements of multi-dimensional arrays using pointer arithmetic and array subscripting.

Q: How do I dynamically allocate memory for a character array in C?

A: You can dynamically allocate memory for a character array using the malloc() function, like so: char *arr = (char *) malloc(size);. Don't forget to free the memory when you're done with it.

Q: What is a null-terminated string, and how is it different from an array of characters?

A: A null-terminated string is a sequence of characters terminated by the null character (\0), which marks the end of the string. An array of characters, on the other hand, is simply a contiguous block of memory allocated for storing characters without any explicit end marker.

Q: How do I check if a given character is present in a string using pointers?

A: You can iterate through the string using a pointer and compare each character until you find the target character or reach the null character (\0). If the target character is found, return true; otherwise, return false.

Q: How do I swap two characters in a string using pointers?

A: You can use two pointers to access the two characters you want to swap and then exchange their values using temporary variables or directly by swapping the addresses they point to.

Q: How do I find the index of the first occurrence of a specific character in a string using pointers?

A: You can iterate through the string using a pointer and compare each character until you find the target character or reach the null character (\0). If the target character is found, return its index; otherwise, return -1.