⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pointer.cpp

📁 Basic pointer examples in C/C++. Show you how to understand * and & symbol in C/C++. Written and exe
💻 CPP
字号:
#include <iostream>
using namespace std;
using namespace System;

int main()
{
	int Nbr;
	cout << "Address of Nbr: " << &Nbr;			//& access the address of Nbr in memory location
	Console::WriteLine();

	cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";

	int *Number(0);								//to initialize, only can assign value 0, not other
												//OR, int *Number = 0;
	
	cout << "Address of Number: " << &Number;	//& access the addr of Number in memory loc
	cout << "\nValue of Number: " << Number;	//value of Number
	//cout << "\nValue of       : " << *Number;	//give (error?) bcoz pointing to memory location 0
	Console::WriteLine();

	cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";

	int A = 123;
	int *B = &A;								//(#)assign value of (address of A) to B | B is a pointer
	
	//int *B;							//
	//*B = &A;							//these give error

	//int *B;									//alternative to (#)
	//B = &A;									//
	
	//int *B;									//assign value to (*B). don't bother the address and value of B,
	//*B=72;									//just the value of (*B) i.e. value pointed by B

	cout << "Address of A: " << &A;				//address of A
	cout << "\nValue of A: " << A;				//value of A
	cout << "\nAddress of B: " << &B;			//address of B
	cout << "\nValue of B: " << B;				//value of B, equal to address of A
	cout << "\nValue at memory location pointed by B is " << *B;//value of B=address of A,but B is a pointer, so this show the memory contain at location B
	Console::WriteLine();

	int *C;
	*C=81;

	cout << "Address of C: " << &C;				//address of C
	cout << "\nValue of C: " << C;				//value of C
	cout << "\nValue at memory location pointed by C is " << *C;

	cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
	cout << "\n\n\nSo, '&' means 'address of' and '*pointer' means 'memory contain at location pointed by pointer";

    int value = 26;
    int *pointer;
    int **pointerToPointer;

    pointer = &value;
    pointerToPointer = &pointer;

    Console::WriteLine("  \n\n\n\n\nValue   = {0}", value);
    Console::WriteLine(" *Pointer = {0}", *pointer);
    Console::WriteLine("**Pointer = {0}\n", **pointerToPointer);
    
    value = 4805;

    Console::WriteLine("After changing the value of the main variable...");
    Console::WriteLine("  Value   = {0}", value);
    Console::WriteLine(" *Pointer = {0}", *pointer);
    Console::WriteLine("**Pointer = {0}\n", **pointerToPointer);

    *pointer = -728;

    Console::WriteLine("After changing the value of the main variable...");
    Console::WriteLine("  Value   = {0}", value);
    Console::WriteLine(" *Pointer = {0}", *pointer);
    Console::WriteLine("**Pointer = {0}\n", **pointerToPointer);

    **pointerToPointer = 945580;

    Console::WriteLine("After changing the value of the main variable...");
    Console::WriteLine("  Value   = {0}", value);
    Console::WriteLine(" *Pointer = {0}", *pointer);
    Console::WriteLine("**Pointer = {0}\n", **pointerToPointer);



	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -