VOOZH about

URL: https://www.geeksforgeeks.org/c/whats-difference-between-char-s-and-char-s-in-c/

⇱ What's difference between char s[] and char *s in C? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

What's difference between char s[] and char *s in C?

Last Updated : 23 Jul, 2025

Consider below two statements in C. What is the difference between the two?

 char s[] = "geeksquiz";
 char *s = "geeksquiz";

Below are the key differences:

Aspectchar a[10] = "geek";char *p = "geek";
Naturea is an arrayp is a pointer variable
Sizesizeof(a) = 10 bytes (32-bit Systems)sizeof(p) = 4 bytes (32-bit Systems)
Address Comparisona and &a are the samep and &p are not the same
Memory Location"geek" is stored in the stack section of memoryp is stored in stack, but "geek" is stored in code section
Reassignmenta = "hello"; β†’ Invalid
Reason: a is an address and cannot be reassigned to another address (string constant)
p = "india"; β†’ Valid
Incrementa++ β†’ Invalidp++ β†’ Valid
Modificationa[0] = 'b'; β†’ Validp[0] = 'k'; β†’ Invalid
Reason: Code section is read-only

The statements 'char s[] = "geeksquiz"' creates a character array which is like any other array and we can do all array operations. The only special thing about this array is, although we have initialized it with 9 elements, its size is 10 (Compiler automatically adds '\0') 


Output

10
jeeksquiz

The statement 'char *s = "geeksquiz"' creates a string literal. The string literal is stored in the read-only part of memory by most of the compilers. The C and C++ standards say that string literals have static storage duration, any attempt at modifying them gives undefined behavior. 
s is just a pointer and like any other pointer stores address of string literal. 


Output

8

Running above program may generate a warning also "warning: deprecated conversion from string constant to β€˜char*’". This warning occurs because s is not a const pointer, but stores address of the read-only location. The warning can be avoided by the pointer to const.


Output

8
Comment