Ad

String literals are stored as read-only (const), and any attempt to modify the pointer returned will result in a SEGFAULT (crash) in most current computing environments.

Obvserving the following assembly output from GCC:

	.file	"tst.c"
	.text
	.section	.rodata
.LC0:
	.string	"Hello World."
	.text
	.globl	Hi
	.type	Hi, @function
Hi:

You can see string literal referred to by label .LC0 is defined within section '.rodata' meaning ready-only data.

Therefore Hi() should have a return type of const char* to avert the possibility of such faulty access occurring.

Code
Diff
  • const char* Hi (void)
    {
      return("Hello World.");
    }
    
    char *oopsHi(void)
    {
      return ("Hello World.");
    }
    
    • char* Hi (void)
    • const char* Hi (void)
    • {
    • return("Hello World.");
    • }
    • return("Hello World.");
    • }
    • char *oopsHi(void)
    • {
    • return ("Hello World.");
    • }