Memory points not working.

I've been researching everywhere for this issue, yet I can't see what's wrong.
This is the source code.
--------------------------------------------------------------------------------

#include <iostream>
#include <windows.h>

using namespace std;
int main(){
cout << "Location / Memory finder." << endl;
cout << "Version 1 Alpha." << endl;
system("PAUSE");
cout << "Type in a word." << endl;
char word;
cin >> word;
Sleep(500);
cout << "Locating memory of word/string... " << endl;
string wordlocation = &word;
cout <<"Memory location found: Located at memory point "<< wordlocation << endl;
system("PAUSE");


}
--------------------------------------------------------------------------------

When I type a word, it gives me the first letter of the word, then a symbol, or nothing.

Can somebody explain to me why this won't work?

Because the code is wrong.

string wordlocation = &word

doesn't mean anything. You are assigning a pointer value to a std::string. I'm surprised it compiles.

Give a read through the tutorial on pointers
http://www.cplusplus.com/doc/tutorial/pointers/

then consider the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int length_of_cstring( const char* p )
  {
  int length = 0;
  while (*p != '\0')
    {
    length += 1;
    p += 1;
    }
  return length;
  }

int main()
  {
  cout << length_of_cstring( "Hello world!" ) << endl;
  cout << length_of_cstring( "74" ) << endl;
  return 0;
  }

If you can wrap your head around that, then you've got pointers pretty good.

Also, see http://www.cplusplus.com/faq/sequences/strings/c-strings-and-pointers/

Yes, this is a lot of reading, but it'll go by pretty quickly. Your problem is that you still haven't quite wrapped your brain around pointers.

Good luck!
> You are assigning a pointer value to a std::string. I'm surprised it compiles.
http://www.cplusplus.com/reference/string/string/string/ (4)

@OP: char holds just 1 character
Ah, holy crap, it is a char pointer. Don't know why I was thinking it was something else...
Topic archived. No new replies allowed.