address of char array

Hello,

I was wondering why if I run the code below I get the following, "SPOCK@ a" I get that it will begin at the beginning of the arrays memory location but shouldn't the compiler return an actual address value in hex for the array mem location?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>

using namespace std;

int main(){
    
    char spocks_name [] {'S','P', 'O', 'C', 'K'};
    
    cout << "\nThe first char is: " << spocks_name[0] << endl;
    cout << "\nThe first char is: " << spocks_name[1] << endl;
    cout << "\nThe first char is: " << spocks_name[2] << endl;
    cout << "\nThe first char is: " << spocks_name[3] << endl;
    cout << "\nThe first char is: " << spocks_name[4] << endl;
    cout << endl;
    
    cout << "address of spocks name is: " << spocks_name << endl;
    
    cout << endl;
    return 0;
}


console output:

The first char is: S

The first char is: P

The first char is: O

The first char is: C

The first char is: K

address of spocks name is: SPOCK@ a
This function is being called: ostream& operator<< (ostream& os, const char* s)

What does that function do? It puts the character being pointed to into the output stream, and all the characters that follow it in memory, until it finds a zero value.

shouldn't the compiler return an actual address value in hex for the array mem location?

That's simply not what the ostream& operator<< (ostream& os, const char* s) operator does.

However, there are operators that take pointers of other types that DO output the value of that pointer, rather than output what is being pointed at.

The difference is a convenience for the programmer; it's common to want to output some char values and common to have a pointer to the first one.

Last edited on
I see. That makes sense.

Thanks for clarifying that.
Topic archived. No new replies allowed.