conceptual of pointer in C++

CONCEPTUAL OF POINTER IN C++
Here some code example of pointer in C++ :
#include
void main()
{
int data = 7; //
int *pdata = &data;
cout<<"data = "<<
cout<<"&data = "<<&data<
cout<<"*pdata = "<<*pdata<
cout<<"pdata = "<<
cout<<"&pdata = "<<&pdata<
getch();
}
Output :
Application description :
Program allocated an identifier data in memory with integer data type which needed 2 byte space in block memory (you can check the memory used of identifier with sizeof(int) function), beside that program either allocated an identifier with integer data type but have pointer behavior that is pdata.
Pdata will initialized with default value to address of identifier data in memory with using command :
Int *pdata = &data
‘&’ sign in data will return value of address identifier data in memory
Pointer behavior will follow and duplicate identifier data which pointed by pointer
Form program above we can describe that :
Identifier data :
  1. Have an address = 0x22ff6c (syntax : cout << &data;)
  2. Have a value = 7 (syntax : cout << data;)
pointer pdata : (has pointed to identifier data)
  1. Have an information address of identifier data = 0x22ff6c (syntax : cout << pdata)
  2. Have an information value of identifier data = 7 (syntax : cout << *pdata)
// both information in above result from pointer pointed to identifier data
  1. Beside information above pdata have an information of own address in memory = 0x22ff68 (different with identifier data address) (syntax : cout << &pdata)
Value that input in identifier data will change value in pdata, and value that input in pdata will change value in identifier data
Example :
Int data = 7;
Int *pdata = &data;
*pdata = 8; //pdata value set to 8, and data value will change to 8 also.
Data = 9;//data value set to 9, so pdata value will change to 9 also
Memory allocation in using pointer can seem in figure above:
We assumed memory block as chess board that have 2 dimension array with column (A - L) and row (1 - 12).

No comments: