In C, both char[] and char* are used to represent strings, but there are important differences between them. Generally, C programmers find the following two statements about char[] and char* in C are considered confusing and understanding the difference between them is important.

The first statement puts the literal string “mycplus” in read-only memory and copies the string to newly allocated memory on the stack. This allows you to modify the contents of the array which is holding the string, and it is statically allocated.

The second statement is known as static string allocation and definition. This statement places the string  “mycplus” in the read-only parts of the memory and making p_name a pointer to that string. Also, any writing operation on this memory location are illegal. However, the pointer itself can be changed to point to a new location.

Some of the differences between char [] and char * are as follow:

  1. Array vs. Pointers: The main difference between both the statements is that s_name is a character array where as p_name is a character pointer type variable.
  2. Memory Allocation: The sizeof(s_name) and sizeof(p_name) are different because memory handling is different in both statements as explained above.
  3. Variable s_name and it’s address &s_name is same. where as p_name and &p_name are not same.
  4. Initialization: The statement s_name="Program" is invalid while p_name="Program" is a invalid C statement. You can initialize a character array char[] when declaring it, and it must have a fixed size. However, you can initialize a character pointer char* with a string literal or by dynamically allocating memory.
  5. Modifiability: char[]: The contents of a character array can be modified directly because it’s an array with allocated memory. char*: If the pointer is pointing to a string literal (like in the example above), attempting to modify the contents will result in undefined behavior.

Above statements can be verified by using the following C Program.

The output of the program looks similar to the following (depending upon compiler and OS used). The program might show some warnings such as

If you try to un-comment the following statement: //s_name=”Program”; the program will terminate and show an error message similar one of the following.

In summary, char[] is a fixed-size array that can be modified directly, while char* is a pointer that can be used to point to a string in memory, and it provides more flexibility in terms of dynamic memory allocation.

Here’s Must-Read List of C programming books for beginners

C Programming Language

C Programming Language, 2nd Edition

With over 600 5-star reviews on Amazon, readers agree that C Programming Language, 2nd Edition by Brian Kernighan and Dennis Ritchie is the best C Programming book for Beginners. The authors present the complete guide to ANSI standard C language programming. Written by the developers of C, this new version helps readers keep up with the finalized ANSI standard for C while showing how to take advantage of C’s rich set of operators, economy of expression, improved control flow, and data structures.


21st Century C

21st Century C: C Tips from the New School

Throw out your old ideas about C and get to know a programming language that’s substantially outgrown its origins. With this revised edition of 21st Century C, you’ll discover up-to-date techniques missing from other C tutorials, whether you’re new to the language or just getting reacquainted.


C Programming Absolute Beginners Guide

C Programming Absolute Beginner’s Guide

Greg Perry is the author of over 75 computer books and known for bringing programming topics down to the beginner’s level. His book C Programming Absolute Beginner’s Guide, is today’s best beginner’s guide to writing C programs–and to learning skills to use with practically any language. Its simple, practical instructions will help you start creating useful, reliable C code, from games to mobile apps. Plus, it’s fully updated for the new C11 standard and today’s free, open source tools!


C Programming - A Modern Approach

C Programming: A Modern Approach, 2nd Edition

KN King tackles on some C standard library specifics header by header in his C Programming: A Modern Approach book. The second edition maintains all the book’s popular features and brings it up to date with coverage of the C99 standard. The new edition also adds a significant number of exercises and longer programming projects, and includes extensive revisions and updates.


Programming Arduino

Programming Arduino: Getting Started with Sketches

Simon Monk, a Ph.D. in software engineering, writes this great little book for learning to program the arduino using C language. This bestselling guide explains how to write well-crafted sketches using Arduino’s modified C language. You will learn how to configure hardware and software, develop your own sketches, work with built-in and custom Arduino libraries, and explore the Internet of Things—all with no prior programming experience required!