c++ beginner s5-pointer

pointer

void manipulate(double *pValue) {
    // dValue : 123.4
    *pValue = 10.0;
    // dValue : 10
}

int main() {

    int nValue = 8;
    //  nValue : 8
    // *nValue : error
    // &nValue : 0x7fff5b87f288 add_1

    int *pnValue = &nValue;
    //  pnValue : 0x7fff5b87f288 add_1
    // *pnValue : 8
    // &pnValue : 0x7fff5b87f280 add_2

    int **ppnValue = &pnValue;
    //   ppnValue : 0x7fff5b87f280 add_2
    //  *ppnValue : 0x7fff5b87f288 add_1
    // **ppnValue : 8
    //  &ppnValue : 0x7fff5b87f278 add_3

    double dValue = 123.4;
    // dValue : 123.4
    manipulate(&dValue);
    // dValue : 10

    return 0;
}

8

nValue

*pnValue

**ppnValue

0x7fff5b87f288

&nValue

pnValue

*ppnValue

0x7fff5b87f280

&pnValue

ppnValue

0x7fff5b87f278

&ppnValue

Arithmatic

double value1 = (double)7/2;

Pointer & array

string texts[] = { "one", "two", "three" };
// texts    : 0x7fff5c44c320
// texts[0] : "one"

string *pTexts = texts;
for (int i = 0; i < sizeof(texts) / sizeof(string); i++) {
    cout << pTexts[i] << " " << flush;
}

for (int i = 0; i < sizeof(texts) / sizeof(string); i++, pTexts++) {
    cout << *pTexts << " " << flush;
}

Last updated

Was this helpful?