C++ Syntax Pointers To Primitive Type

 

- Info:

- Pointer to Primitive Type is Pointer that holds address at which some Primitive Type is stored.

- Declaration of constant pointer to constant unsigned long primitive type would look like this:

const unsigned long * const ptcul = &cul;

 

- Examples:

- Create C++ Console Application Test.cpp.

 

Test.cpp

#include <stdio.h>

int main () {

int i = 10;

int (*pti) = &i; //(*pti) and (pti[0]) behave complitely the same as (i).

// PRIMITIVE TYPE.----------------------------------------------

printf("%i\n", (i) ); //10 int variable.

printf("%p\n", &(i) ); //2293620 Address of int variable.

printf("%i\n", *&(i) ); //10 int variable.

printf("%p\n", &*&(i) ); //2293620 Address of int variable.

// POINTER TO PRIMITIVE TYPE.-----------------------------------

printf("\n");

printf("%i\n", (*pti) ); //10 int variable.

printf("%p\n", &(*pti) ); //2293620 Address of int variable.

printf("%i\n", *&(*pti) ); //10 int variable.

printf("%p\n", &*&(*pti) ); //2293620 Address of int variable.

printf("\n");

printf("%i\n", (pti[0])); // 10 int variable.

printf("%p\n", &(pti[0])); // 2293620 Address of int variable.

printf("%i\n", *&(pti[0])); // 10 int variable.

printf("%p\n", &*&(pti[0])); // 2293620 Address of int variable.

}

 

Test.cpp

#include <stdio.h>

int main () {

int a[4] = {10,11,12,13};

int (*pti) = a; //=&a[0]; // (*pti) and (pti[0]) behave complitely the same as a[0].

// PRIMITIVE TYPE.------------------------------------------------

printf("%i\n", (a[0]) ); // 10 int variable.

printf("%p\n", &(a[0]) ); // 0022FF60 Address of int variable.

// POINTER TO PRIMITIVE TYPE.-------------------------------------

printf("\n");

printf("%i\n", (*pti) ); // 10 int variable.

printf("%p\n", &(*pti) ); // 0022FF60 Address of int variable.

printf("%i\n", (*(pti+1)) ); // 11 int variable.

printf("%p\n", &(*(pti+1)) ); // 0022FF64 Address of int variable.

printf("\n");

printf("%i\n", (pti[0]) ); // 10 int variable.

printf("%p\n", &(pti[0]) ); // 0022FF60 Address of int variable.

printf("%i\n", (pti[1]) ); // 11 int variable.

printf("%p\n", &(pti[1]) ); // 0022FF64 Address of int variable.

}