YOUR AD GOES HERE

Understanding char* and Strings in CS50

Published 05, Aug 2025

vlogize


Description:
Learn how to effectively work with `char*` and string literals in C programming with CS50. Discover the differences and get clear explanations for common questions.
---
This video is based on the question https://stackoverflow.com/q/76665161/ asked by the user 'King Brain' ( https://stackoverflow.com/u/21975651/ ) and on the answer https://stackoverflow.com/a/76665254/ provided by the user 'Vlad from Moscow' ( https://stackoverflow.com/u/2877241/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Trouble understanding char* and string in CS50

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding char* and Strings in CS50: A Comprehensive Guide

When diving into the world of programming, especially in C as taught in CS50, understanding strings and character pointers can be quite a challenge for beginners. Many learners grapple with how to effectively use char* and string literals, leading to confusion and misconceptions. Today, we'll break down this complex topic and provide clarity on some common questions you might have.

The Problem: Clarifying char* and String Literals

It's essential to first understand what a string is in C. A string in C is essentially an array of characters, with its memory arranged sequentially. To determine the location of a string, you simply need to point to the address of the first character, and remember that a string is terminated by the special character \0. Let's explore a couple of closely related questions that often arise:

Does the declaration char* s = "HI!"; create an array of 4 characters or merely a pointer?

Why does the code char* name = "BEN"; printf("%c %c\n", *(name + 1), *name + 1); produce two different outputs instead of the expected single character?

Solution: Explanation of the Concepts

Understanding Pointer Initialization

Let's first dissect the initialization char* s = "HI!";. This declaration can indeed be a source of confusion. Here’s what happens step by step:

String Literal Creation: The string literal "HI!" is created and occupies an array of 4 characters in memory, where the last character is the null character \0. In C, this appears as a character array of type char[4], whereas in C+ + it would strictly be const char[4] due to different rules regarding string literals.

Pointer Assignment: The pointer s is then assigned the address of the first element of this character array. The pointer does not create a new array; it merely points at the existing string literal.

Checking Size: You can check the size of the string literal to confirm it with a simple printf:

[[See Video to Reveal this Text or Code Snippet]]

Understanding Character Dereferencing and Pointer Arithmetic

Next, let's explain this snippet of code:

[[See Video to Reveal this Text or Code Snippet]]

Here’s the breakdown of each component:

Pointer Arithmetic: The expression name + 1 results in a pointer to the second character of the string literal "BEN", which is 'E'. By dereferencing this pointer using *(name + 1), we get the character 'E' as expected. This is equivalent to name[1].

Dereferencing and Incrementing: The expression *name gets the first character 'B'. When we use *name + 1, we are essentially taking the ASCII value of 'B', adding 1 to it (resulting in the value of 'C'), and then printing that. This is why you see two distinct outputs: 'E' and 'C'.

Simplifying Output with String Literals

Interestingly, you can still achieve the same output using the string literal directly, bypassing the need for pointers entirely. Here are a few alternate ways of writing the printf statement:

Using pointer arithmetic directly on the string:

[[See Video to Reveal this Text or Code Snippet]]

Utilizing the subscript operator:

[[See Video to Reveal this Text or Code Snippet]]

More unconventional but valid usage would be:

[[See Video to Reveal this Text or Code Snippet]]

Conclusion

Understanding char* and how strings operate in C can significantly enhance your programming skills. By comprehending how pointers work, especially in conjunction with string literals, you'll be more equipped to handle character data throughout your programming journey. Remember, strings are just a series of characters, and a char* is merely a pointer to access these characters in a linear format! Keep practicing,

Releted More Videos

You May Also Like

YOUR AD GOES HERE

YOUR AD GOES HERE