Note_20150526

本文深入探讨了C++中指针的基本概念、声明与使用方法,包括如何通过指针访问和修改内存中的数据。通过具体代码示例,详细展示了如何将值赋给指针、通过指针进行值的读写操作以及不同类型指针的用法。重点突出了C++指针在内存管理中的重要性及其实现复杂性的关键特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++ basics

/************************/

In C++, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.

/************************/

C++ does not recognize the end of the line as a terminator.

/************************/

C++ KEY WORDS

\asm               else            new                         this
auto                enum         operator                 throw
bool                explicit       private                    true
break             export         protected               try
case               extern         public                    typedef
catch              false           register                  typeid
char                float           reinterpret_cast    typename
class              for               return                     union
const              friend         short                       unsigned
const_cast    goto            signed                   using
continue        if                  sizeof                     virtual
default           inline          static                       void
delete            int                static_cast             volatile
do                   long            struct                      wchar_t
double           mutable      switch                    while
dynamic_cast               namespace              template

/************************/

A local variable is known only to the function in which  it is declared.

/************************/

A formal parameter is a local variable that receives the value of an arument.

/************************/

Now, it is the most interesting part-pointer.

Pointers are without doubt one of the most important-and troublesome-aspects of C++. In fact, a large measure of C++'s power is derived from pointers. They allow C++ to support such things as linked lists and dynamic memory allocation.

A pointer is a variable that contains the address of another object.

To declare P to be a pointer to an integer, use the declaration: int *P

                                 a pointer to  a float, use the declaration: float *P

There are twp special operators that are used with pointers: * and &.

Let's see the simple example.


#include<iostream>

using namespace std;

int main()

{

    int balance;

    int *balptr;

    int value;

   

   balance = 300;

   balptr  = &balance;

   value = *balptr;

   cout << " balance is : "<< value << '\n'<<endl;

   return 0;

}

The output is shown here.
balance is: 3200

/************************/

The base type of pointer is important. It determine the how many bytes of address the compiler would transfer for the pointer types.

/************************/

Assigning Values Through a Pointer

#include <iostream>
using namespace std;
int main()
{
int *p, num;
p = &num;
*p = 100;
cout << num << ' ';
(*p)++;//Here is call back the value which is stored in the p.
cout << num << ' ';
(*p)--;
cout << num << '\n';
return 0;
}
The output from the program is shown here.
100 101 100

/************************/





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值