Where does const char* get the pointer to a memory address?
Asked Answered
U

4

24

This may be simple question, but why does a const char* not need a memory address to point to?

Example:

const char* a = "Anthony";

and not:

const char *a = // Address to const char

like any other types do?

Ultranationalism answered 18/4, 2020 at 8:2 Comment(3)
What makes you think string literals don't have memory addresses?Cordeiro
Agreed. I wouldn't expect anyone asking this question to even know value categories exist, let alone that they have names.Brodeur
Please do not ask questions tagged with C and C++. As we can observe, the answers are now C++ specific and the comments derail again about the differences between both languages. There are so many differences by now that it's difficult to ask a question that actually has the same valid answer for both languages. Decide which language you want to use before asking, please.Demoniac
B
29

You can imagine this declaration

const char* a = "Anthony";

the following way

const char string_literal[] = "Anthony";

const char *a = string_literal;

That is the compiler creates an array of characters with the static storage duration that stores the string "Anthony" and the address of the first character of the array (due to the implicit conversion of array designators to pointers to their first characters) is assigned to the pointer a.

Here is a demonstrative program that shows that string literals are character arrays.

#include <iostream>
#include <type_traits>

decltype( auto ) f()
{
    return ( "Anthony" );
}

template <size_t N>
void g( const char ( &s )[N] )
{
    std::cout << s << '\n';
}

int main() 
{
    decltype( auto ) r = f();

    std::cout << "The size of the referenced array is "
              << std::extent<std::remove_reference<decltype( r )>::type>::value
              << '\n';

    g( r );

    return 0;
}

The program output is

The size of the referenced array is 8
Anthony

The size of the string literal (of the array that stores the string literal) is equal to 8 because the string includes also the terminating zero character '\0'.

In the demonstrative program the expression

std::extent<std::remove_reference<decltype( r )>::type>::value

may be substituted for just the expression

sizeof( r )
Blankbook answered 18/4, 2020 at 8:10 Comment(0)
H
5

why does a const char don't need a memory address to point to?*

It does.

A C-string literal like

"Anthony"

is decayed to the address of its 1st character. Like, BTW; any array in C does.

Herl answered 18/4, 2020 at 8:4 Comment(9)
More specifically, it's of type const char[8] (in C++, might be char [8] in C, not sure) and like all built-in arrays, when using it as a value it decays to a pointer to its first element.Ie
@NikosC.: Thanks for reminding me of the most important magic verb in this context! ;)Herl
Thanks for the answer! I was wondering where does it get the memory from.Ultranationalism
@Weidelix: "where does it get the memory from" the compiler generates code to allocate enough memory for it, most likely during program startup/initialisation.Herl
@alk, isn't this part of the binary, living in the data segment, as opposed to being allocated by generated code?Scurrilous
@rid: Yes, also a common approach, but left to the implementation.Herl
Can't speak for C, but I'm pretty sure that C++ doesn't specify where a sting literal must be stored. Just went digging. If there's a rule, it's buried somewhere odd and far away from any mention of "string literal".Brodeur
@NikosC. char [8] in C: c-faq.com/ansi/strlitnotconst.htmlProponent
More guesswork. Compilers don't create avoidable work for themselves or for the executing program. Literals are stored statically.Cordeiro
A
2

It does need a memory address, and it HAS a memory address. In your example it's simply the memory address of the beginning of the string. It's the same with any other array variable that's initialized at compile time, for instance "int array [] = {0, 1, 2, 3};".

If you used a binary editor to look at the executable, you would see the string "Anthony" in there. If you put the line "printf ("a is at %p\n", (void *)a);" in your program, then compile & run it, you'll see the address.

Abramson answered 18/4, 2020 at 22:2 Comment(0)
S
0

"Why does const char* don't need a pointer to a memory address?"

In fact, It does need an memory address to point to.

const char* a means a is a pointer to a string literal or character constant.

A pointer always requires an address to point to because it is the nature of a pointer to point to a specific object in memory. So, a and any other pointer to const char does too.

A string literal like "Hi My Name is Alfred!" by an assignment like:

const char* a;
a = "Hi My Name is Alfred!";

decays to a pointer to the address of the first element of the string literal.

Means in turn, a gets assigned by the address of the first element of the string literal "Hi My Name is Alfred!" which could be stored anywhere in memory dependent on the execution environment.

It is not in the might of a programmer where a string literal is exactly stored. Your assignment is only to assign and handle the respective pointer appropriately.

Sharie answered 18/4, 2020 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.