指针知识(七):空(void)指针_无效(Invalid)指针_空值(null)指针

 

1.  void指针

 (鉴于对容易混淆的指针属性,在此贴上英文解释,英文表达更加直接)

 

[The void type of pointer is a special type of pointer. In C++, void represents the absence of type. Therefore, voidpointers are pointers that point to a value that has no type (and thus also an undetermined length and undetermined dereferencing properties).

This gives void pointers a great flexibility, by being able to point to any data type, from an integer value or a float to a string of characters. In exchange, they have a great limitation: the data pointed by them cannot be directly dereferenced (which is logical, since we have no type to dereference to), and for that reason, any address in a voidpointer needs to be transformed into some other pointer type that points to a concrete data type before being dereferenced.]

 

void 类型指针是特殊类型的指针。在 c++ 中,void 表示没有任何类型,因此 void 指针指向的值是没有类型的(不确定的长度和值属性)。

这使得 void 类型指针的灵活性非常强,void 指针可以指向任意类型的数据,可以是整数,浮点数甚至字符串。

唯一个限制被指向的数值不可以被直接 dereference(不可以对它们直接使用星号*),因为它的长度是不定的,因此,必须使用类型转换操作或赋值操作来把void 指针指向一个具体的数据类型。

它的应用之一是被用来给函数传递通用参数:

 

#include <iostream>

using namespace std;

void increase (void* data , int psize)

{

  switch(psize)

  {

    case sizeof(char):

      char* pchar;

      pchar = (char*)data;

      ++(*pchar);

      break;

    case sizeof(short):

      (*((short*)data))++;  //和上面的声明效果一样

      break;

    case sizeof(long):

      (*((long*)data))++;

      break;

  }

}

int main()

{

  char a = 5;

  short b = 7;

  long c = 9;

  increase( &a, sizeof(a));

  increase( &b, sizeof(b));

  increase( &c, sizeof(c));

  cout<<(int)a<<", "<<b<<", "<<c<<'\n';

 

  return 0;

}

输出:

6, 8, 10

 

2.   (Invalid)无效指针and 空值(null)指针

 

 [In principle, pointers are meant to point to valid addresses, such as the address of a variable or the address of an element in an array. But pointers can actually point to any address, including addresses that do not refer to any valid element. Typical examples of this are uninitialized pointers and pointers to nonexistent elements of an array:]

原则上,指针是指向有效地址的,例如一个变量的地址或是数组中某个元素的地址。但是,指针也可以指向空地址,包括没有任何有效值的地址——特殊的例子是:没有初始化的指针和指向数组中不存在的元素,如

  int* p;          //未初始化的指针

  int myarray[10];      

  int* q = myarray + 20;  //不包含在数组中 的值

 

[Neither p nor q point to addresses known to contain a value, but none of the above statements causes an error. In C++, pointers are allowed to take any address value, no matter whether there actually is something at that address or not. What can cause an error is to dereference such a pointer (i.e., actually accessing the value they point to). ]

p 和q 指向的地址单元包含的都是空值,但上面的语句没有一个报错的。在c++中,指针允许是任何的地址,不管这个地址中是否有值。当从这样的指针中获取指向的值时会有错。

 

[But, sometimes, a pointer really needs to explicitly point to nowhere, and not just an invalid address. For such cases, there exists a special value that any pointer type can take: the null pointer value. This value can be expressed in C++ in two ways: either with an integer value of zero, or with the nullptr keyword:]

但是有时候,我们确实需要指针不指向任何地方,而不仅仅是无效的地址。如这样的情况:任何指针类型都可以取的值:the null pointer value.

这个值在c++中有两种表示方法:

 

 

1. int* p = 0;

2. int* q = nullptr;

 

 

 

 

[Here, both p and q are null pointers, meaning that they explicitly point to nowhere, and they both actually compare equal: all null pointers compare equal to other null pointers. It is also quite usual to see the defined constant NULL be used in older code to refer to the null pointer value:]

里的 p 和 q 都是空指针,不指向任何地方,它们是等价的:所有的空指针都是等价的

在过去我们也经常看见把指针声明为常量 NULLint* r = NULL;

 

[NULL is defined in several headers of the standard library, and is defined as an alias of some null pointer constant value (such as 0 or nullptr).]

NULL 被定义作为空指针常量的别名(such as 0 or nullptr).

 

[Do not confuse null pointers with void pointers! A null pointer is a value that any pointer can take to represent that it is pointing to "nowhere", while a void pointer is a type of pointer that can point to somewhere without a specific type. One refers to the value stored in the pointer, and the other to the type of data it points to.]

注意:不要混淆 空值 指针和 void 指针!一个空值指针是任何指针都能取到的值,表示指针不指向任何地方;一个 void 指针是一种指针类型,可以指向没有类型的值。一个代表存在指针中的值;一个代表指针指向值的类型。



转载于:https://www.cnblogs.com/guozqzzu/p/3597024.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值