sid_com
sid_com

Reputation: 25137

C: string-literal - question

Is only in char *ptr = "Hello World" a string literal or are both "Hello World" string literals?

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    char array[] = { "Hello World" };
    char *ptr = "Hello World";

    printf( "%s\n", array );
    printf( "%s\n", ptr );

    printf( "%c\n", *array );
    printf( "%c\n", *ptr );

    printf( "%c\n", array[1] );
    printf( "%c\n", ptr[1] );

    return EXIT_SUCCESS;
}

# Hello World
# Hello World
# H
# H
# e
# e

Upvotes: 0

Views: 1055

Answers (7)

ThiefMaster
ThiefMaster

Reputation: 318748

"Hello world" is always a string literal, no matter what you do with it.

char *ptr = "Hello World"; defines a pointer which points to space which is often not modifiable (so you should handle it like a const char * and your compiler might actually complain if you don't).

char array[] = { "Hello World" }; creates the array on the stack so it's modifiable and roughly equal to char array[sizeof("Hello World")]; strcpy(array, "Hello World");

Upvotes: 3

Prasoon Saurav
Prasoon Saurav

Reputation: 92924

"Hello World" is a string literal.

In case of char *ptr = "Hello World"; ptr is pointing to "Hello World" which is present in read only location. Any attempt to modify the it via ptr would invoke undefined behaviour.

Whereas in char array[] = { "Hello World" }; content of the string literal is copied onto a stack. You are allowed to modify those content without invoking UB.

BTW ISO C99 (Section 6.4.5/6) says

It is unspecified whether these arrays are distinct provided their elements have the appropriate values.

That means it is unspecified whether the compiler treats two(or more) occurrences of "Hello World" distinct.

Upvotes: 2

Kevin
Kevin

Reputation: 570

When you declare char array[] you are declaring an array of chars (which is accessible to be both read and written), and this array is initialized to some sequence of characters

If you declare char * ptr, you are declaring a pointer that points directly to some constant literal(not a copy) and you can just read it

Upvotes: 0

Erik
Erik

Reputation: 91320

"Hello World" is a string literal - no matter whether you assign it to a char * or copy it to a char []

char * ptr = "Hello World"; should really be const char * ptr = "Hello World"; - String literals may be unmodifiable (in readonly memory), and using a const char * is an extra safeguard against modification.

char array[] = { "Hello World" }; is safe - this copies the string literal from the potential readonly memory into a local variable.

Upvotes: 5

DavidMFrey
DavidMFrey

Reputation: 1668

This is compiled to a string literal in the read only section.

        .section        .rodata
.LC0:
        .string "Hello World"

Both occurences are combined into one array... at least on my machine with gcc.

Upvotes: 0

Jens Gustedt
Jens Gustedt

Reputation: 78973

Only the "Hello World" themselves are string literals.

ptr is a pointer to char initialized with the address of the first element of the string literal.

array is an array and not a literal.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799430

"Hello World" is a string literal, regardless of where in the code it appears. Neither ptr nor array are string literals.

Upvotes: 0

Related Questions