Correct memory address?

Hi, I'm creating a program that asks the user for a number between 0 and 27 and returns the letter in that number position of the alphabet. So far so good, but now I want to return the memory address of the letter. I don't quite think I'm doing it right... Here's my code:

#include "stdafx.h"
#include <iostream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
char alphabet[26] = {'a', 'b', 'c', 'd', 'e','f','g','h', 'i', 'j',
'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int n;
int * letterpointer;

cout << "Enter a number between 0 and 27 and I will tell you \nwhat letter holds that position in the alphabet... " << endl;
cin >> n;

cout << endl;

cout << "Position " << n << " of the alphabet is occupied by: " << alphabet[n - 1] << endl;

cout << "The current memory address of " << alphabet[n-1] << " is: ";

letterpointer = &n; //letterpointer gets the address of n
cout << letterpointer << endl;

cout << endl;
system("pause");
return 0;
}

So as you can see, letterpointer is getting the address of n, but is n the same as alphabet[n-1]? I tried doing letterpointer = &alphabet[n-1] but that doesnt seem to be working...
closed account (zb0S216C)
Use the correct types. A pointer to a char should be char *, not int *.
letterpointer = &n;

This statement in no way correlates to the array. All that you're doing here is assigning letterpointer to the location of n. All that n stores the the users input. It's best to do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main( )
{

    char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    int Pos( 0 );
    std::cin >> Pos;

    char *Letter( Alphabet + ( Pos - 1 ) );

    std::cout << *Letter << std::endl;

    return( 0 );
}


And please, in the future, could you use [code_][/code] tags (without the underscore).

Wazzak
Last edited on
You're doing weird things with your indexing. Array with 26 elements will have subscripts 0 through 25. So you don't need to do anything to user input to make it match the array, and you shouldn't ask for 0 to 27. Gonna be out of bounds on 2 of those letters with your current setup. As for the pointer issue, I don't know pointers all that well and I'm on my phone and driving so can't look at it that well. Moschops seems to be the pointer expert around here
Oh ok, thanks! And ill do tags next time :)
This might help.

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
27
28
29
30
31
#include <iostream>
#include <limits>
using namespace std;


int main()
{
char alphabet[26] = {'a', 'b', 'c', 'd', 'e','f','g','h', 'i', 'j',
'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int n;
char * letterpointer=alphabet;

cout << "Enter a number between 0 and 27 and I will tell you \nwhat letter holds that position in the alphabet... " << endl;
cin >> n;

cout << endl;

cout << "Position " << n << " of the alphabet is occupied by: " << letterpointer[n - 1] << endl;

cout << "The current memory address of " << letterpointer[n-1] << " is: ";


cout << (void*)letterpointer[n-1] << endl;

cout << endl;
delete letterpointer;
cin.sync();
cout<<"Press Enter Key To Exit"<<endl;
cin.ignore( numeric_limits<streamsize>::max(),'\n' );
return 0;
}
Last edited on
Hmmm... well i changed the pointer type to char and assigned &alphabet[n-1] to letterpointer but I don't think i'm getting the right output... Here's the revised code:

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
27
28
29
#include "stdafx.h"
#include <iostream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	char alphabet[26] = {'a', 'b', 'c', 'd', 'e','f','g','h', 'i', 'j',
		'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
	int n;
	char * letterpointer;

	cout << "Enter a number between 0 and 27 and I will tell you \nwhat letter holds that position in the alphabet... " << endl;
	cin >> n;

	cout << endl;

	cout << "Position " << n << " of the alphabet is occupied by: " << alphabet[n - 1] << endl;	// subtract 1 because arrays begin counting with 0
	
	cout << "The current memory address of " << alphabet[n-1] << " is: ";
	
	letterpointer = &alphabet[n-1];			//letterpointer gets the address of n
	cout << letterpointer << endl;

	cout << endl;
	system("pause");
	return 0;
}


What am I doing wrong?
closed account (zb0S216C)
Hydrasin wrote:
cout << letterpointer << endl;

...prints the address of the pointed-to char. Is that what you expected?

Wazzak
Last edited on
That is what I expect, but the output doesn't look like an address. The output is the rest of the alphabet and some symbols.
closed account (zb0S216C)
Does it resemble something like this: 01AFDD4? If so, that's an address (in hexadecimal) without the 0x prefix. If not, can you show us?

Wazzak
When cout is fed a pointer to char via operator<<, it interprets it as a pointer to c-string. Casting it to another poniter type would have the desired effect.

cout << (void*)letterpointer << endl ;
Okay, quick lesson in pointers.

* refers to pointing to something
& is the memory address.

The best way to understand how pointers works is to play with them. Create a Project called "Test" which you can use to play around with code and see how things work. A long time ago I did this to learn about pointers and how they work, they are really helpful once you get a firm grasp of them but the only real way I feel to understand them is to see them work. So here is the code and the comments after the cout is what the cout comes out as so that you can see first hand what everything does.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	int ii = 5;
	int *pi = (int*)ii;
	int *pi2 = &ii;

	cout << &ii << endl;	// (Memory address of this variable set to 4 bytes)
	cout << pi << endl;	// 5
	cout << &pi << endl;	// The memory addess of the int pointer
	cout << pi2 << endl;	// Since is holding the memory addess of ii .. ii's memory addess
	cout << &pi2 << endl;	// The actual memory address of ip2
	cout << *pi2 << endl;	// 5

	ii = 10;

	cout << pi << endl;	// It will still say 5
	cout << *pi2 << endl;	// This will now say 10 even though we didn't touch pi2 


Now understand that each variable HAS to have a memory address setup for it when it is created, even void pointers do (void pointers are another lesson but VERY IMPORTANT). So every variables will ALWAYS have a different memory address associated with it.

If you want to experiment more, there are more pointers you can play around with and learn about like

 
int **i;


Like I said, the best way to learn is to pop it into a test project and see it for yourself. Learning from someone else doing it is good, but learning from seeing for yourself is the fastest and best way to learn!
Last edited on
Topic archived. No new replies allowed.