C Pointers and Strings

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Pointers and Strings?

A C string is a char array ending with '\0', and string functions work through char *. A pointer to a string literal (char *s = "hi") must be treated as read-only; use a char array when you need to modify the text.

Remember
const char *s = "hello";   /* literal: do not modify */
char word[] = "hello";        /* writable copy          */
while (*p) { p++; }           /* stop at '\0'           */

Cheat Sheet

Keep these facts handy while you work with Pointers and Strings in C.

TopicDetail
LiteralRead-only; use const char *
Arraychar buf[] = "text"; is modifiable
End'\0' terminates
Lengthstrlen(s) or walk to '\0'

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Pointers and Functions.

Example 1 — Measure and uppercase a string

A pointer walks the characters until the terminator.

C
#include <stdio.h>
#include <ctype.h>

static size_t my_len(const char *s) {
    const char *p = s;
    while (*p) p++;
    return (size_t)(p - s);
}

int main(void) {
    char word[] = "hello";

    printf("%s has %zu chars\n", word, my_len(word));
    for (char *p = word; *p; p++) {
        *p = (char)toupper((unsigned char)*p);
    }
    printf("%s\n", word);
    return 0;
}

How It Works

  1. my_len advances p until it hits '\0' and subtracts the start.
  2. word is a writable array, so uppercase edits are legal.
  3. Casting to unsigned char keeps toupper safe for all values.

Edge Cases & Pitfalls

Check these before relying on Pointers and Strings in real programs.

lit

Modifying a literal

char *s = "abc"; s[0] = 'X'; is undefined behavior. Use char s[] = "abc";.

nul

Missing terminator

A char array without '\0' is not a string; printf("%s") runs past its end.

Key Takeaways

  • Literal: treat as const.
  • Array: copy when you need to edit.
  • Terminator: '\0' ends the string.

One line: A C string is a char array ending with '\0', and string functions work through char *.

Frequently Asked Questions

char s[] = "hi" creates a modifiable array containing the characters. char *s = "hi" points to a string literal that must not be modified.
Loop while (*dst++ = *src++); until the terminator is copied, or call strcpy when the destination is large enough.
Continue with "C Pointers and Functions" next. Pass pointers into functions and return pointers from them. The C Pointers hub lists every topic in order.

Next: Pointers and Functions

Pass pointers into functions and return pointers from them.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful