Character Pointers and Functions in C
Character Pointers and Functions in C
char *pointer_name;
https://www.tutorialspoint.com/cprogramming/c_character_pointers_and_functions.htm 1/10
6/16/24, 1:14 PM Character Pointers and Functions in C
#include <stdio.h>
int main() {
// Declare two variables
char x = 'P';
char arr[] = "TutorialsPoint";
// Printing values
printf("Value of x : %c\n", *ptr_x);
printf("Value of arr: %s\n", ptr_arr);
return 0;
}
Output
Value of x : P
Value of arr: TutorialsPoint
https://www.tutorialspoint.com/cprogramming/c_character_pointers_and_functions.htm 2/10
6/16/24, 1:14 PM Character Pointers and Functions in C
The string is a NULL terminated array of characters. The last element in the above
array is a NULL character (\0).
Declare a pointer of char type and assign it the address of the character at the 0th
position −
Remember that the name of the array itself is the address of 0th element.
A string may be declared using a pointer instead of an array variable (no square
brackets).
This causes the string to be stored in the memory, and its address stored in ptr. We
can traverse the string by incrementing the ptr.
while(*ptr != '\0'){
printf("%c", *ptr);
ptr++;
}
Example
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_character_pointers_and_functions.htm 3/10
6/16/24, 1:14 PM Character Pointers and Functions in C
while(*ptr != '\0'){
printf("%c", *ptr);
ptr++;
}
}
Output
Example
#include <stdio.h>
int main(){
printf("%s", ptr);
}
Output
https://www.tutorialspoint.com/cprogramming/c_character_pointers_and_functions.htm 4/10
6/16/24, 1:14 PM Character Pointers and Functions in C
The strlen() function returns the length, i.e. the number of characters in a string.
The prototype of strlen() function is as follows −
int strlen(char *)
Example 1
The following code shows how you can print the length of a string −
#include <stdio.h>
#include <string.h>
int main(){
return 0;
}
When you run this code, it will produce the following output −
Example 2
Effectively, the strlen() function computes the string length as per the user-defined
function str_len() as shown below −
#include <stdio.h>
#include <string.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_character_pointers_and_functions.htm 5/10
6/16/24, 1:14 PM Character Pointers and Functions in C
return 0;
}
When you run this code, it will produce the following output −
Example 1
The following example shows how you can use the strcpy() function −
#include <stdio.h>
#include <string.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_character_pointers_and_functions.htm 6/10
6/16/24, 1:14 PM Character Pointers and Functions in C
strcpy(ptr1, ptr);
printf("%s", ptr1);
return 0;
}
The strcpy() function returns the pointer to the destination string ptr1.
Example 2
Internally, the strcpy() function implements the following logic in the user-defined
str_cpy() function −
#include <stdio.h>
#include <string.h>
int main(){
str_cpy(ptr1, ptr);
printf("%s", ptr1);
return 0;
}
d[i] = '\0';
}
When you runt his code, it will produce the following output −
https://www.tutorialspoint.com/cprogramming/c_character_pointers_and_functions.htm 7/10
6/16/24, 1:14 PM Character Pointers and Functions in C
The function copies each character from the source string to the destination till the
NULL character "\0" is reached. After the loop, it adds a "\0" character at the end of
the destination array.
The usual comparison operators (<, >, <=, >=, ==, and !=) are not allowed to be
used for comparing two strings. Instead, we need to use strcmp() function from the
"string.h" header file. The prototype of this function is as follows −
Example 1
The following example demonstrates how you can use the strcmp() function in a C
program −
#include <stdio.h>
#include <string.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_character_pointers_and_functions.htm 8/10
6/16/24, 1:14 PM Character Pointers and Functions in C
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;
}
Change s1 to BACK and run the code again. Now, you will get the following output
−
Example 2
You can obtain a similar result using the user-defined function str_cmp(), as shown
in the following code −
#include <stdio.h>
#include <string.h>
int main(){
if (ret == 0)
printf("Both strings are identical\n");
else if (ret > 0)
printf("The first string appears after the second string\n");
else
https://www.tutorialspoint.com/cprogramming/c_character_pointers_and_functions.htm 9/10
6/16/24, 1:14 PM Character Pointers and Functions in C
return 0;
}
return 0;
}
When you run this code, it will produce the following output −
The str_cmp() function compares the characters at the same index in a string till the
characters in either string are exhausted or the characters are equal.
At the time of detecting unequal characters at the same index, the difference in their
ASCII values is returned. It returns "0" when the loop is terminated.
https://www.tutorialspoint.com/cprogramming/c_character_pointers_and_functions.htm 10/10