The difference here is that
char *s = "Hello world";
will place "Hello world"
in the read-only parts of the memory, and making s
a pointer to that makes any writing operation on this memory illegal.
While doing:
char s[] = "Hello world";
puts the literal string in read-only memory and copies the string to newly allocated memory on the stack. Thus making
s[0] = 'J';
legal.
Related Posts:
- Return char[]/string from a function [duplicate]
- Return char[]/string from a function
- Does C have a string type?
- The difference between char * and char[] [duplicate]
- How does strtok() split the string into tokens in C?
- How to convert an int to string in C?
- What is a string of hexadecimal digits?
- How to convert an int to string in C?
- How to convert a string to integer in C?
- Get a substring of a char* [duplicate]
- char *array and char array[]
- How do I create an array of strings in C?
- Get a substring of a char*
- Expression must be a modifiable L-value
- How do I properly compare strings in C?
- How to convert an int to string in C?
- How to convert a std::string to const char* or char*
- Returning string from C function
- How do I create an array of strings in C?
- Char Comparison in C
- How to check if a string is a number?
- Difference between char* and const char*?
- How can I convert const char* to string and then back to char*?
- How do I lowercase a string in C?
- How to print a char array in C through printf?
- How to clear all the elements of array in C?
- Constant pointer vs Pointer to constant
- How do I concatenate const/literal strings in C?
- Reversing a string in C
- How to remove the character at a given index from a string in C?
- Excess elements in char array initializer error
- Split string with delimiters in C
- C char* to int conversion
- char pointers: invalid conversion from ‘char*’ to ‘char’?
- Allocating string with malloc
- Removing trailing newline character from fgets() input
- Allocating char array using malloc
- How to concatenate string and int in C?
- How do I check if a string contains a certain character?
- Convert Char to String in C
- What causes a segmentation fault (core dump) to occur in C?
- How to use symbols of extended ASCII table in C?
- How to convert a char to a String?
- typedef struct vs struct definitions [duplicate]
- Cannot figure out how to use getchar(); in C
- Why am I getting “void value not ignored as it ought to be”?
- 1 = false and 0 = true?
- c stack smashing detected
- What is an unsigned char?
- Convert char to int in C and C++
- What causes the Broken Pipe Error?
- C: correct usage of strtok_r
- Why does the terminal show “^[[A” “^[[B” “^[[C” “^[[D” when pressing the arrow keys in Ubuntu?
- Write to .txt file?
- What does “collect2: error: ld returned 1 exit status” mean?
- Warning comparison between pointer and integer
- Using ssize_t vs int
- Why a warning of “control reaches end of non-void function” for the main function?
- How to remove .html from URL?
- How to convert an int to string in C?
- How do I use extern to share variables between source files?
- C read file line by line
- What is the difference between a static and const variable?
- What is a bus error? Is it different from a segmentation fault?
- Get the current time in C
- where does stdio.o live in linux machine?
- Using Dynamic Memory allocation for arrays
- conflicting types error when compiling c program using gcc
- Get the current time in C
- What is a file with extension .a?
- error: indirection requires pointer operand (‘int’ invalid)
- Finding the length of a Character Array in C
- Valgrind: Invalid read of size 1
- Zero an array in C code
- Encrypt with private key and decrypt with public key
- strcmp giving segmentation fault
- What does “pointer being freed was not allocated” mean exactly?
- How to use timer in C?
- Convert char array to a int number in C
- What is the difference between stdin and STDIN_FILENO?
- How to simply convert a float to a string in c?
- What is the difference between char array and char pointer in C?
- How to copy a char array in C?
- hash function for string
- What happens during a “relocation has invalid symbol index” error?
- What are .axf files?
- Error: “Access not within mapped region at address” (Valgrind)
- printf format specifiers for uint32_t and size_t
- How to remove first character from C-string?
- How to convert string to float?
- What is an simple example of copy_from_user
- C char array initialization
- How does the fscanf function work?
- char array not assignable
- getline() vs. fgets(): Control memory allocation
- What is char ** in C? [duplicate]
- segmentation fault using scanf
- Segmentation fault- strcat
- How to format strings using printf() to get equal length in the output
- C: warning: excess elements in array initializer; near initialization for ‘xxx’ ; expects ‘char *’, but has type ‘int’