Chris Lee
Chris Lee

Reputation: 49

I am really confused with understanding array pointer in C

#include <stdio.h>

int main (void)
{
    char *str = "Hello"; //defining and initializing the str pointer, which is directing to 'H'.

    printf("%s\n", str);
    printf("%p\n", str);
    return 0;
}

The Result is:

Hello
0000000000404000

My question is where did 0000000000404000 come from?

Upvotes: 0

Views: 89

Answers (2)

Saurav Sahu
Saurav Sahu

Reputation: 13934

In Bjarne Stroustrup words:

%s The argument is taken to be a string (character pointer), and characters from the string are printed until a null character or until the number of characters indicated by the precision specification is reached; however, if the precision is 0 or missing, all characters up to a null are printed;

%p The argument is taken to be a pointer. The representation printed is implementation dependent;

Upvotes: 0

Brandon83
Brandon83

Reputation: 206

The format specifier %p will print the address contained within the char* str variable whereas the %s specifier will print the actual string literal Hello. The address in memory 0x00000000 00404000 is where Hello resides.

Upvotes: 2

Related Questions