c语言100位整数变量声明_C ++程序动态声明一个整数变量并打印其内存地址

c语言100位整数变量声明

Here, we will learn how we can declare an integer variable dynamically and how to print address of declared memory block?

在这里,我们将学习如何动态声明整数变量,以及如何打印声明的内存块的地址?

In C++ programming, we can declare memory at run time for any type variable like char, int, float, integer array etc. To declare memory at run time (dynamically), we use new operator. I would recommend reading about new operator first.

在C ++编程中,我们可以在运行时为char,int,float,integer array等任何类型的变量声明内存。要在运行时(动态)声明内存,请使用new运算符。 我建议先阅读有关新运算符的信息

Consider the following program:

考虑以下程序:

#include <iostream>

using namespace std;

int main()
{
	//integer pointer declartion
	//It will contain the address of dynamically created 
	//memory blocks for an integer variable 
	int *ptr;
	
	//new will create memory blocks at run time 
	//for an integer variable
	ptr=new int;

	cout<<"Address of ptr: "<<(&ptr)<<endl;
	cout<<"Address that ptr contains: "<<ptr<<endl;

	//again assigning it run time
	ptr=new int;

	cout<<"Address of ptr: "<<(&ptr)<<endl;
	cout<<"Address that ptr contains: "<<ptr<<endl;

	//deallocating...
	delete (ptr);

	return 0;	
}


输出量 (Output)

    Address of ptr: 0x7ffebe87bb98
    Address that ptr contains: 0x2b31e4097c20
    Address of ptr: 0x7ffebe87bb98
    Address that ptr contains: 0x2b31e4098c50

Here we are declaring an integer pointer ptr and printing the address of ptr by using &ptr along with stored dynamically allocated memory block.

在这里,我们声明的整数指针PTR以及通过使用&PTR与存储动态分配的存储块沿着打印PTR的地址。

After that we are declaring memory for integer again and printing the same.

之后,我们再次声明整数的内存并打印该整数。

From this example - we can understand memory blocks are allocating dynamically, each time different memory blocks are storing in the pointer ptr.

从这个例子中,我们可以理解每次将不同的存储块存储在指针ptr中时,存储块都是动态分配的。

翻译自: https://www.includehelp.com/code-snippets/cpp-declare-integer-variable-dynamically-print-the-memory-addresses.aspx

c语言100位整数变量声明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值