Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C Language Dictionary

  1. Home
  2. C Language Dictionary
  3. Pointers and Strings

Pointers and Strings

Since: C89(1989)

In C, strings are represented as arrays of char with a null character ('\0') at the end. Strings are commonly accessed through char * pointers, and the standard string manipulation functions are provided in <string.h>.

Syntax

char str[20] = "Hello";
char str[] = "Hello"; // Size is determined automatically (6 bytes including the null character).

// Declare a pointer to a string literal (not modifiable).
const char *p = "Hello";

// Common standard string functions (requires #include <string.h>).
strlen(str) // Returns the length of the string (excluding the null character).
strcpy(dest, src) // Copies a string.
strncpy(dest, src, n) // Copies up to n characters.
strcat(dest, src) // Concatenates two strings.
strcmp(str1, str2) // Compares two strings.
strchr(str, ch) // Searches for a character and returns its address.
strstr(str, sub) // Searches for a substring and returns its address.

Common String Functions

FunctionDescription
strlen(s)Returns the length of the string, excluding the null character.
strcpy(dest, src)Copies the string in src to dest. The dest buffer must be large enough to hold the result.
strncpy(dest, src, n)Copies up to n characters from src to dest. Recommended to prevent buffer overflows.
strcat(dest, src)Appends src to the end of dest. The dest buffer must have enough space for the combined string.
strcmp(s1, s2)Compares two strings. Returns 0 if equal, a negative value if s1 is less, or a positive value if s1 is greater.
strchr(s, c)Returns a pointer to the first occurrence of character c in the string, or NULL if not found.
strstr(s, sub)Returns a pointer to the first occurrence of substring sub in the string.

Sample Code

sample_pointer_string.c
#include <stdio.h>
#include <string.h>

int main(void) {
    // A char array string can be modified.
    char greeting[20] = "Hello";
    greeting[0] = 'h'; // Overwrite the first character.
    printf("%s\n", greeting); // Prints 'hello'.

    // const char * is a pointer to a string literal (not modifiable).
    const char *lang = "C language";
    printf("%s\n", lang);

    printf("Length: %zu\n", strlen(greeting)); // Prints 'Length: 5'.

    // Copy a string.
    char copy[20];
    strncpy(copy, greeting, sizeof(copy) - 1);
    copy[sizeof(copy) - 1] = '\0'; // Ensure null termination for safety.
    printf("Copy: %s\n", copy); // Prints 'Copy: hello'.

    // Compare strings (you cannot use == for this).
    char s1[] = "sample_data_x";
    char s2[] = "sample_data_x";
    if (strcmp(s1, s2) == 0) {
        printf("The strings are equal.\n");
    }

    // Search for a character.
    char sentence[] = "Hello, World!";
    char *found = strchr(sentence, 'W');
    if (found != NULL) {
        printf("Position of 'W': %s\n", found); // Prints 'World!'.
    }

    // Iterate over characters one by one using a pointer.
    char *p = sentence;
    int vowels = 0;
    while (*p != '\0') {
        if (*p == 'a' || *p == 'e' || *p == 'i' || *p == 'o' || *p == 'u') {
            vowels++;
        }
        p++;
    }
    printf("Vowel count: %d\n", vowels);

    return 0;
}

Run the following command:

gcc pointer_string.c -o pointer_string
./pointer_string
hello
C language
Length: 5
Copy: hello
The strings are equal.
Position of 'W': World!
Vowel count: 3

Common Mistakes

Common Mistake: Using == to Compare Strings

Using == to compare strings compares the addresses (pointer values), not the contents. Even if two strings have the same content, they may not match. Always use strcmp() to compare string contents.

string_eq_ng.c
#include <stdio.h>

int main(void) {
    char s1[] = "item_x";
    char s2[] = "item_x";

    /* NG: == compares addresses (may evaluate as not equal) */
    if (s1 == s2) {
        printf("Equal\n");
    } else {
        printf("Not equal (different addresses).\n");
    }
    return 0;
}

Run the following command:

gcc string_eq_ng.c -o string_eq_ng
./string_eq_ng
Not equal (different addresses).
string_eq_ok.c
#include <stdio.h>
#include <string.h>

int main(void) {
    char s1[] = "item_x";
    char s2[] = "item_x";

    /* OK: use strcmp() to compare contents */
    if (strcmp(s1, s2) == 0) {
        printf("Contents are equal.\n");
    }
    return 0;
}

Run the following command:

gcc string_eq_ok.c -o string_eq_ok
./string_eq_ok
Contents are equal.

Notes

C has no dedicated string type — all strings are represented as char arrays terminated by a null character. Using == to compare strings compares their addresses, not their contents. Always use strcmp() to compare string values.

strcpy() and strcat() do not check buffer sizes, which can lead to buffer overflows if the destination is too small. For security, it is recommended to use the size-limited variants strncpy() and strncat().

For safe string input, use fgets(). For printf/scanf format specifiers, see printf() / fprintf().

If you find any errors or copyright issues, please .