the address of char pointer is nonsense

Please help!
Is it a valid char pointer address or I have done something wrong in the code?

The output looks like this:

w♂ (     w

♀ (     ♀

 (


(The strange signs are a male and female signs in the first and second rows respectively)

The code is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>


using namespace std;

int main()
{
     //see if char pointer has a memory address??????
    char y='w';
    char *s;
    s=&y;
    cout<<s<<"     "<<*s<<endl<<endl;

    (s++);
    cout<<s<<"     "<<*s<<endl<<endl;

    (s++);
    cout<<s<<"     "<<*s;

    cin.get();

    return 0;
}


The last *s does not contain anything as there is nothing on the output.
But pointer s really has this address?

Thank you for help!
Gyo
dint get ur problem ,are u trying to find out the address of the pointer?
compare
1
2
3
char y = 'w';
char *s = &y;
cout << s;

with
1
2
3
char y[] = "hello world";
char *s = y;
cout << s;

They both do "cout << my_char_pointer". In the first case it doesn't do what you want, in the second, it does. Whoever built C++ standard library decided that the second case is going to be more common (obviously).
If you want to get the numeric vaule of a char*, do "cout << (void*)s;"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;

int main() 
{
    char y='e';
    int x=0;
    
    char *s;
    s=&y;
    int *a;
    a=&x;
    
    cout<<y<<endl<<x<<endl<<*s<<(int)s<<endl<<a<<endl<<*a;
    cin.get();
    return 0;
}
Thanks guys!
Yes, I wanted to find out what the address is of a single char point. Because it has to be somewhere in the memory.

Hamsterman, thanks, I tried "cout<<(void*)s;", and it worked.
But could you explain it why the simple "s" makes a nonsense on the output and "(void*)s" gives me the address I need?
Thanks again, it was a big help.
Badshah,
I used "(int*)s" at the last cout.
It also worked giving me back the address.

0x28ff0b      w

0x28ff0c      ♀

0x28ff0d


the code is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    char y='w';
    char *s;
    s=&y;
    cout<<(void*)s<<"     "<<*s<<endl<<endl;

    (s++);
    cout<<(void*)s<<"     "<<*s<<endl<<endl;

    (s++);
    cout<<(int*)s<<"     "<<*s;

    cin.get();

    return 0;



but when I did not use "*" after "(int)s" I got back a different number.
Sorry if it is trivial for you, but I do not understand why it is different?

the output is

0x28ff0b      w

0x28ff0c      ♀

2686733


the code is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    char y='w';
    char *s;
    s=&y;
    cout<<(void*)s<<"     "<<*s<<endl<<endl;

    (s++);
    cout<<(void*)s<<"     "<<*s<<endl<<endl;

    (s++);
    cout<<(int)s<<"     "<<*s;

    cin.get();

    return 0;


Thanks, and sorry for bothering you with such a simple question.
Gyo
The << operator behaves differently depending on what type of variable you give it. See my previous post. In both cases s is a pointer to char, so it is natural that << will behave the same way. It has no way to know that the first one is a pointer to a single char and the second one is to an array. So cout << any_char_pointer; will try to print a string that the pointer points to.
Likewise, cout << any_other_pointer; will print the address the pointer points to in hexadecimal. Also, cout << any_integer; will print the decimal representation of the value stored in any_integer.
Which is why (int)s looks different from (int*)s. The only difference is that the first one is in decimal and the second one - in hexadecimal (which is marked be a 0x prefix).
i hope hamsterman answered ur question well enough :D
Thanks.
Probably I am stupid to understand it but I don't know why the simple "s" doesn't give me the hexadecimal address and why "(void*)s" or "(int*)s" does?
Since "*s" should give back the value stored in pointer s and "s" should give back the address the pointer s pointing to.
So where from "s" gives me that strange output?
 w♂ (     w

I am not going to ask more question!
:)
Thanks a lot!
Maybe running this would help you understand:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
 
void foo(int f) {
   std::cout << "foo has been passed an integer\n";
}
 
void foo(void* f) {
   std::cout << "foo has been passed a pointer to something\n";
}
 
void foo(char* f) {
   std::cout << "foo has been passed a string\n";
}
 
int main(){
   char ch = 'w';
   int i = 5;
   foo(i);
   foo(&i);
   foo(ch);
   foo(&ch);
   foo("hello");
}
Overloading operators works the same way. It is for designers of standard library to decide what and how is printed.
Topic archived. No new replies allowed.