0

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
1
  • 1
    I see 8 string literals: "Hello World" twice, "%s\n" also twice, and "%c\n" four times. If they occupy 8 distinct regions of memory or 3 (or anything in between) is unspecified by the Standard: implementations are free to do as they like. Commented Mar 22, 2011 at 16:02

7 Answers 7

5

"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.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually it should really be const char * const in this example, as ptr is not modified either. :)
@GrahamS: That'd just add confusion to what's important here, that the literal should be treated as const.
True enough, but I think it is an important sub-point. If we are declaring char pointers to string literals then we should generally use const char * const MyLiteral = "My Literal Text"; as that says MyLiteral points to "My Literal Text" and always will.
3

"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");

1 Comment

Does char array[] = "hello world" store in stack , i mean does each character h , e , l , l , o , w , o ,r ,l ,d , gets stored in stack , and is char array [] = "hello world" is equivalent to char array[] = {h,e,l,l,o,w,o,r,l,d} ;
2

"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.

Comments

1

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.

Comments

0

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.