C++ 指针

C++ 指针

在C中我们已经学习过指针的,所以,这里比不过多介绍,我们着重介绍指针的几个特性吧。

Pointer arithmetics

++mychar;
++myshort;
++mylong;

指针++并非单纯的地址值加上1,而是它所指向的单位加上相应的单位长度。

这个概念是你一定要明确的。

Pointers and const

指针对于常量可以访问其地址,但不能通过获得的地址修改它的值,程序给常量的内存一定的保护,这点一定要明确。

int x;
int y = 10;
const int * p = &y;
x = *p;          // ok: reading p
*p = x;          // error: modifying p, which is const-qualified

Pointers to pointers

指向指针的指针,可能这个有点绕,其实很简单,简单看个图你就可以理解。

char a;
char * b;
char ** c;
a = 'z';
b = &a;
c = &b;

可以看出 在声明时是 **c,但在使用时仅仅使用 c 即可,这个道理是你需要明确的。

void pointers

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

可以看出 void类型指针是很灵活的,它可以指向任何一种形式的数据。但在使用它时要声明其指针类型。

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 to 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 void pointer needs to be transformed into some other pointer type that points to a concrete data type before being dereferenced.

并不能被直接复制,这个道理你应该明确的。

// increaser
#include <iostream>
using namespace std;

void increase (void* data, int psize)
{
  if ( psize == sizeof(char) )
  { char* pchar; pchar=(char*)data; ++(*pchar); }
  else if (psize == sizeof(int) )
  { int* pint; pint=(int*)data; ++(*pint); }
}

int main ()
{
  char a = 'x';
  int b = 1602;
  increase (&a,sizeof(a));
  increase (&b,sizeof(b));
  cout << a << ", " << b << '\n';
  return 0;
}

Invalid pointers and null pointers

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;               // uninitialized pointer (local variable)

int myarray[10];
int * q = myarray+20;  // element out of bounds 

对未出初始化的指针一定要注意,就算未初始化,它里面也存储着某个值

可以写成这样:int * r = NULL;

区分void和null
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.

null是不知道其值,但知道该指针的类型,但void连其类型也不知道,是指向一个类型的指针,可以指向任何地方不用非得有一个特殊的类型。

Pointers to functions

函数也可以被指针指向,在声明其指针时。

#include <iostream>
#include <string>
using namespace std;
int addtion(int a,int b)
{
    return a+b;
}
int subtraction(int a,int b)
{
    return a-b;
}
int operatrion(int a,int b,int (*fun)(int a,int b))
{
    return (*fun)(a,b);
}
int main()
{
    int (*minus)(int,int) = subtraction;

    cout << operatrion(1,2,minus);

    return 0;
}

在声明函数的指针时,在后面需要附上其参数的类型,之后直接跟上函数名了,这个没有什么不同的,之后传递改指针就好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值