Using 'char' to represent text

*** Newbie question ***

1). char day[] = "Thursday";
std::cout << "Today is " << day << std::endl;

2). char Day[9] = "Thursday";
std::cout << "Today is " << Day << std::endl;

Both of the above methods produce the same output; "Today is Thursday".

I have also seen programmers use char pointers to represent text:

3). char* DAY = Day;

So why does this work? If this were a 'real' pointer, wouldn't the code be written like so:

4). char* DAY = &Day;

In example 3, 'DAY' returns the value of 'Day' rather than a pointer to 'DAY', so I figured why the need for the pointer(*) at all if this doesn't actually operate as a pointer:

5). char DAY = Day;

But this produces the error:

" error C2440: 'initializing': cannot convert from 'char [9]' to 'char' "

What exactly is going on here? Why do some people use pointers(*) to represent text and why do these not actually operate as pointers?
Last edited on
3). char* DAY = Day;
So why does this work?

Because array names almost always decay to "pointer to first element of array".

So
char* DAY = Day;
is the same as
char* DAY = &Day[0];

Try this.
char* DAY = &Day[5];
std::cout << "Today is " << DAY << std::endl;
Ah okay. So:

char* DAY = Day;

or

char* DAY = &Day[0];

Simply stores all elements of the character array 'Day' into the char* 'DAY'. Whilst:

char* DAY = &Day[5];

Stores all elements after and including the fifth element of the character array 'Day' into the char* 'DAY'.

Apologies for my beginner questions, but why does the char* 'DAY' not actually store the memory address of 'Day', like a normal pointer would? For example:

int x = 1;
void* pointer = &x;
std::cout << "Today is " << pointer << std::endl;

Outputs "Today is 0x0093f84c", which is obviously a pointer to the memory address of the variable 'x'. Why does the same not happen in our example? Our example:

char Day[9] = "Thursday";
char* DAY = &Day[0];
std::cout << "Today is " << DAY << std::endl;

Outputs 'Today is Thursday". But if I use a 'void' pointer rather than a 'char' pointer to the character array 'Day':

char Day[9] = "Thursday";
void* pointer = &Day;
std::cout << "Today is " << pointer << std::endl;

Then the output is "Today is 0x00bdfbcc" , which is what I would have expected a pointer to do. So I guess my question is why does a char* output the actual value of 'Day', whilst the void* outputs the memory address of 'Day'.

Again sorry if this question seems really simple, I just can't really get my head around why these two examples do not operate in the same way.

Last edited on
> Simply stores all elements of the character array 'Day' into the char* 'DAY'. Whilst:
No, there are no additional copies of the elements of the array.

It's like a postcard with your address on it.
Writing your address on a 2nd postcard doesn't make a new house for you.

> Apologies for my beginner questions, but why does the char* 'DAY' not actually store the memory address of 'Day', like a normal pointer would?
It does store a memory address like a normal pointer.

I think you're confusing the 'special case' in std::cout where if you have a char* or const char * pointer, you get what the pointer points to (the string) rather than the 0xnnnnnnnn representation of a memory address you would otherwise get with any other type of pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main ()
{
    char today[] = "Thursday";
    char *day1 = today;
    char *day2 = &today[5];

    std::cout << "Today is " << today << std::endl;
    std::cout << "Today is " << day1 << std::endl;
    std::cout << "Today is " << day2 << std::endl;

    std::cout << "Today is " << reinterpret_cast<void*>(today) << std::endl;
    std::cout << "Today is " << reinterpret_cast<void*>(day1) << std::endl;
    std::cout << "Today is " << reinterpret_cast<void*>(day2) << std::endl;
}
Note that when you assign a pointer say A to another pointer say B then the pointer B isn't pointing to the address of the pointer A but rather it's pointing to what A was pointing to (correct me if I'm wrong).
> I think you're confusing the 'special case' in std::cout where if you have a char* or const char * pointer, you get what the pointer points to (the string) rather than the 0xnnnnnnnn representation of a memory address you would otherwise get with any other type of pointer.

Yes you are right. So would it be correct to say that the char*/const char* is still pointing to the memory address, but 'std::cout' will effectively output 'what the pointer points to' rather than its memory address.

And that if any other type of pointer is used (as opposed to char*/const char*), this would not be the case, and the pointer would simply output the memory address in 'cout' rather than what the pointer points to?
Last edited on
Yes calioranged, that's correct.
Thanks for your help salem c
Topic archived. No new replies allowed.