char pointers

I am writing a program that calls the memory address of different variable types. I am having troubles with getting the address locations for my array and my char pointers. Here is what I have so far:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>

using namespace std;

int main()
{
	const int NUMBER_LIST = 3;
	int number = 5;
	string name  = "Broc";
	char color[]= "white";
	double cost = 2.45;
	int list [NUMBER_LIST] = {1,2,3};

	int* pNumber = 0;
	pNumber = &number;

	string* pName = 0;
	pName = &name;

	char* pColor = &color[10];


	double* pCost = 0;
	pCost = &cost;

	int* pList = 0;
	pList = &list[NUMBER_LIST];

	cout <<"Interger has a memory address "<< pNumber<<endl;
	cout <<"String has a memory address "<< pName<<endl;
	for (int i=0; i<3; i++)
	{
		cout <<"Array has a memory address " << pList[i] << endl;
	}
	cout <<"Array has a memory address " << pList[2] << endl;
	for (int i=0; i<5; i++)
	{
	cout <<"Char has a memory address " << pColor[i] << endl;
	}

	cout << "Double has a memory address " << pCost << endl;

	system("pause");
	return 0;
}
char color[]= "white";

color is an array of 6 characters. One for each character in "white" and one for the null terminator at the end to signify the end of the string. This line is the same as saying char color[6] = "white";

So when you do this:
char* pColor = &color[10];

You are getting a pointer to color[10]. Since color is only 6 entries big, there is no 10 entry. Therefore, pColor is a bad pointer.

You are doing the same thing here:

pList = &list[NUMBER_LIST];

'list' only has 3 elements (ie: index [2] is the maximum index). So if you get a pointer to index [3], then that is a pointer to outside the array. IE: it's a bad pointer.


If you want a pointer to the start of the array, then use index 0:

1
2
3
char* pColor = &color[0];  // now pColor points to the first element in the color array.

int* pList = &list[0];  // and pList points to the first element in the list array. 


Although, this can be shortcutted because array names can be implicity cast. So you can just do this:

1
2
3
char* pColor = color;  // same as = &color[0]

int* pList = list;  // same as = &list[0] 



Also, 1 pointer = 1 memory address. When you try to print the addresses on line 30.. you can't loop through pList and pColor as if they were arrays, because they're not arrays. They're a single pointer.

What you're actually doing there is looping through the array they point to.

1
2
3
4
5
6
7
8
9
int* pList = list;  // pList points to the list array

for(int i = 0; i < 3; ++i)
{
  cout << pList[i]; // <- this will not print an address, but will print the contents
    // of the list array.. just as if you were printing list[i]
}

cout << pList;  // <- this will print an address 




char pointers also need to be cast because they are interpretted as strings by iostream. So you'll have to cast them to some other kind of pointer to actually have the address printed.

1
2
cout << pColor;  // will print "white"
cout << (void*)(pColor);  // will print the address of the color array 
First thing this is not right....
char* pColor = &color[10];
you will have to make pointer to point to the first element of the char array..
char* pColor = &color; which is equal to
char* pColor = &(color[0]);

and second thing pList[i] means *(pList+i) which will always give you value rather than address... make it &pList[i].
Last edited on
Thanks for the help and the info provided has helped me better understand pointers. Once again, thank you.
Topic archived. No new replies allowed.