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:

#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;
}

pList[i] is the value of what in that memory address,
pList is the address of the first element,

&pList[i] is the address of that element number i however,

so pList+i is the address still as &pList[i]

Just explore them

The code pName = &name should be either pName = name or pName = &name[0]. What you did was to take the address of the string which is already a pointer.

The code pName = &name should be either pName = name or pName = &name[0].

Actually pName = &name[0]; isn't valid, because you try to convert char* to string*.
Also
char* pColor = &color[10]; will do you no good, since &color[10] references some memory area that color itself doesn't have it. Since char color[]= "white"; is an array of characters of length 6 (including '\0'), &color[10] will point to a totally different memory area, and if printing, it might yeld odious results. If, say, you were to take something like this:

 char* pColor = &color[2];

pColor will than point to the memory area of an array of chars starting from i, e.g. ite

after my changes I get the following output
Interger has a memory address 0x7fff5fbffac4
String has a memory address 0x7fff5fbffaa0
Array has a memory address 0x7fff5fbffa6c
Array has a memory address 0x7fff5fbffa70
Array has a memory address 0x7fff5fbffa74
Array has a memory address 0x7fff5fbffa74
Char has a memory address 0x7fff5fbffa80
Char has a memory address 0x7fff5fbffa88
Char has a memory address 0x7fff5fbffa90
Char has a memory address 0x7fff5fbffa98
Char has a memory address 0x7fff5fbffaa0
Double has a memory address 0x7fff5fbffa98

noting that as what Lucaci Andrew said "&color[10] will point to a totally different memory area" I changed it to &pColor+i to point to the first letter address

Re Andrew's response - doh! Thanks! Brain fart there! Absolutely correct! I think I had just responded to a C query so brain was still in K&R mode, not E&S mode... :-) If I could vote down my previous post I would. :rolleyes:

From the department of excuses department: It was late when I read the post! I was tired! My glasses were dirty! Aren't all strings char* types? :lol:

Thanks for all the input, it has helped me a lot in understanding pointers. I was able to get everything to work with your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.