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.
1 2 | char s_name[] = "mycplus"; char *p_name = "mycplus"; |
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:
- 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.
- Memory Allocation: The sizeof(s_name) and sizeof(p_name) are different because memory handling is different in both statements as explained above.
- Variable s_name and it’s address &s_name is same. where as p_name and &p_name are not same.
- Initialization: The statement
s_name="Program"
is invalid whilep_name="Program"
is a invalid C statement. You can initialize a character arraychar[]
when declaring it, and it must have a fixed size. However, you can initialize a character pointerchar*
with a string literal or by dynamically allocating memory. - 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> int main() { char s_name[] = "Hello, World!"; char *p_name = "Hello, World!"; printf("The size of s_name[] is %lu\n",sizeof(s_name)); printf("The size of p_name[] is %lu\n",sizeof(p_name)); printf("Print s_name[]: %lu\n", s_name); printf("Print &s_name[]: %lu\n", &s_name); printf("Print p_name[]: %lu\n", p_name); printf("Print &p_name[]: %lu\n", &p_name); p_name="Program"; //s_name="Program"; return 0; } |
The output of the program looks similar to the following (depending upon compiler and OS used). The program might show some warnings such as
1 | ISO C++ forbids converting a string constant to ‘char*’ |
1 2 3 4 5 6 | The size of s_name[] is 14 The size of p_name[] is 8 Print s_name[]: 140723957079242 Print &s_name[]: 140723957079242 Print p_name[]: 4196116 Print &p_name[]: 140723957079232 |
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.
1 2 3 | 01. error: assignment to expression with array type 02. error: incompatible types in assignment of ‘const char [8]’ to ‘char [14]’ 03. error: array type 'char [14]' is not assignable |
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, 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: 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 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, 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: 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!