0

I read that when you declare strings using a pointer, the pointer contains the memory address of the string literal. Therefore I expected to get the memory address from this code but rather I got some random numbers. Please help me understand why it didn't work.

int main()
{
    char *hi = "Greeting!";
    printf("%p",hi);
    return(0);
}

If the pointer hi contains the memory address of the string literal, then why did it not display the memory address?

5
  • but rather I got some random numbers...What is your output? Commented Mar 22, 2017 at 13:18
  • 2
    What do you expect a memory address to look like if not a bunch of random numbers? Commented Mar 22, 2017 at 13:18
  • 1
    The char pointer points to the address of the string literal. It doesn't contain the address Commented Mar 22, 2017 at 13:20
  • 2
    @SeekAddo, it points to the string, which is another way of saying it contains the string's address. "Points to the address" would be a pointer to a pointer. Commented Mar 22, 2017 at 13:25
  • Sorry guys, the address was 0000000000404000. Because I did not see a letter A through F in it, I was under the impression that it was not a hex. Thanks for your help Commented Mar 23, 2017 at 8:06

3 Answers 3

1

It did work. It's just that you can consider the address as being arbitrarily chosen by the C runtime. hi is a pointer set to the address of the capital G in your string. You own all the memory from hi up to and including the nul-terminator at the end of that string.

Also, use const char *hi = "Greeting!"; rather than char *: the memory starting at hi is read-only. Don't try to modify the string: the behaviour on attempting to do that is undefined.

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

Comments

0

The "random numbers" you got are the Memory addresses. They are not constant, since on each execution of your program, other Memory addresses are used.

Comments

0

A pointer could be represented in several ways. The format string "%p" "writes an implementation defined character sequence defining a pointer" link. In most cases, it's the pointed object's address interpreted as an appropriately sized unsigned integer, which looks like "a bunch of random number".

A user readable pointer representation is generally only useful for debugging. It allows you to compare two different representations (are the pointers the same?) and, in some cases, the relative order and distance between pointers (which pointer comes "first", and how far apart are they?). Interpreting pointers as integers works well in this optic.

It would be helpful to us if you could clarify what output you expected. Perhaps you expected pointers to be zero-based?

Note that, while some compilers might accept your example, it would be wiser to use const char *. Using a char * would allow you to try to modify your string literal, which is undefined behavior.

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.