ProAnswers.org

Difference between a character array and a character pointer in c

difference between a character array and a character pointer.

int main(){
char *cp = “Hello world”;//Character pointer

    printf("%s

",cp);
//will place Hello world in the read-only parts of the memory
//and making cp a pointer to that, making any writing operation on this memory illegal. While doing:
//*(cp+1)=‘a’; is runtime error

    char s[] = "Hello world";//array of characters
    //puts the literal string in read-only memory and copies the string to newly allocated memory on the stack.
   //Making
  
    s[0] = 'J';//correct
    printf("%s",s);
    getch();
}