Home Misc What is the difference between char s[] and char * s in C?

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

by nikoo28
0 comment 3 minutes read

Question:

In C you can do something like:-

char s[] = “Hello World”;

and

char *s = “Hello World”;

What is the difference between the two of them? What happens to the memory during compile time and run-time?

This declaration:

char s[] = "hello";

Creates ONE object – a char array of size 6, called s, initialized with the values ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’. Where this array is allocated in memory, and how long it lives for, depends on where the declaration appears. If the declaration is within a function, it will live until the end of the block that it is declared in, and almost certainly be allocated on the stack; if it’s outside a function, it will probably be stored within an “initialized data segment” that is loaded from the executable file into writable memory when the program is run.

On the other hand, this declaration:

char *s ="hello";

Creates TWO objects:

  • a read-only array of 6 chars containing the values ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’, which has no name and has static storage duration (meaning that it lives for the entire life of the program)
  • and a variable of type pointer-to-char, called s, which is initialized with the location of the first character in that unnamed, read-only array.

The unnamed read-only array is typically located in the “text” segment of the program, which means it is loaded from disk into read-only memory, along with the code itself. The location of the s pointer variable in memory depends on where the declaration appears (just like in the first example).

To understand it even further:-

#include<stdio.h>

int main(void)
{
    char s1[] = "Hello User";
    s1[4] = 'X';    // this is a valid statement, because memory is NOT read only

    printf("%s", s1);    // This will print "HellX User"

    char *s2 = "Hello User";
    s2[4] = 'X';    // this is an INVALID statement, because the memory is read-only

    return 0;
}

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More