c语言指针++_C和C ++中的指针

c语言指针++

C中的指针是什么? (What is Pointer in C?)

A pointer is a variable that holds the address of another variable to which it points. We know that if a variable is defined, it allocates some memory location. To know the address of that memory location, a pointer variable is used. Each data type has its own pointer variable. For example, a pointer to int, a pointer to double, a pointer to char, etc. A memory address is similar to house number or bank locker numbers.

指针是一个变量,它保存指向它的另一个变量的地址。 我们知道,如果定义了一个变量,它将分配一些内存位置。 为了知道该存储单元的地址,使用了一个指针变量。 每种数据类型都有其自己的指针变量。 例如,指向int的指针,指向double的指针,指向char的指针等。内存地址类似于房屋号或银行存物柜号。

指针的优点 (Advantages of Pointers)

  • Length and complexity of program can be reduced.

    程序的长度和复杂度可以降低。
  • Searching/Sorting large size data is much easier.

    搜索/排序大尺寸数据要容易得多。
  • Pointer are use for allocating and deallocating memory dynamically

    指针用于动态分配和释放内存
  • Pointer increases the program’s performance.

    指针可提高程序的性能。

指针的缺点 (Disadvantages of Pointers)

When a pointer goes wrong, it is very difficult to find the bug in the program. If a pointer contains an incorrect value, it can lead to a disaster of great magnitude when used.

当指针出错时,很难在程序中找到错误。 如果指针包含不正确的值,则在使用时可能导致严重的灾难。

When reading a memory location by using this incorrect pointer, a user may be reading an incorrect or a garbage value. For example, consider a scenario in banking in which a customer’s real account is switched with this garbage value. In that case, the customer can become a millionaire or beggar in seconds.

通过使用此不正确的指针读取内存位置时,用户可能正在读取不正确的值或垃圾值。 例如,考虑在银行业务中用此垃圾值切换客户的真实帐户的情况。 在这种情况下,客户可以在几秒钟内成为百万富翁或乞g。

'*'运算符和addressof(&)运算符 (The ‘*’ Operator and addressof(&) operator)

The addressof(&) operator is used to find out the memory address of variable.

addressof(&)运算符用于查找变量的内存地址。

#include<bits/stdc++.h>
int main()
{
int x;
std::cout<<x<<"\n";
std::cout<<"the value of variable x is:"<<x<<"\n";
std::cout<<"The memory addressof a variable x is:"<<&x;
}

The ‘*’ operator is an indirection operator or dereferencing operator. It means the value at a particular address of a variable. It is another way of accessing a value stored at the memory address of a variable.

“ *”运算符是间接运算符或解除引用运算符。 它表示变量特定地址处的值。 这是访问存储在变量的内存地址中的值的另一种方法。

#include<bits/stdc++.h>
int main()
{
int a,b;
a=5;b=10;
std::cout<<"The vaule of variable a is:"<<a<<"\n";
std::cout<<"The address of variable a is:"<<&a<<"\n";
std::cout<<"The value of variable 'a' at address:"<<&a<<"is:"<<*(&a);
}

指针变量 (Pointer Variable)

We know that pointer is a variable that holds the address of the variable to which it points.

我们知道指针是一个变量,它保存它指向的变量的地址。

Let us assume that y is a pointer variable that holds the memory address of a variable x, then it can be assigned by the statement as:

让我们假设y是一个指针变量,它保存变量x的内存地址,那么可以通过以下语句将其分配为:

y = &x.
Creating Pointer Variable

Creating Pointer Variable

创建指针变量

Here, y is pointing to variable x such that y contains the memory address of variable x. In other words, we can say that y is a pointer to variable x.

此处,y指向变量x,以使y包含变量x的内存地址。 换句话说,我们可以说y是指向变量x的指针。

However, y has its own memory address in the system memory as it allocates some space in memory. It is to be noted here that the values 2738 and 1200 are assumed values for memory address.

但是,y在系统内存中有其自己的内存地址,因为它在内存中分配了一些空间。 这里要注意,值2738和1200是存储器地址的假定值。

声明指针变量 (Declaring a Pointer Variable)

data_type*variable_name;

int*marks;, char*ptr1;, double*p1;

初始化指针变量 (Initializing a Pointer Variable)

pointer_variable = &variable_name

p = &a; //storing address of 'a' to pointer variable p.

指针算术 (Pointer Arithmetic)

As we know that pointer is an address which is an integer value, various arithmetic operations can be performed on a pointer just as you can perform on the numeric value.

我们知道指针是一个整数值的地址,可以像对数字值一样对指针执行各种算术运算。

The various arithmetic operations are:

各种算术运算是:

  • Any integer value can be added to pointer variable. It increments the pointer as built-in type variable does.

    可以将任何整数值添加到指针变量。 它像内置类型变量一样递增指针。
  • Any integer type value can be decremented from a pointer variable as a built-in data variable does.

    像内置数据变量一样,可以从指针变量中减去任何整数类型的值。
  • A pointer can be incremented or decremented by using ++ and — urinary operator respectively.

    可以分别使用++和—尿液运算符来增加或减少指针。
  • Two pointer variable cannot be multiplied or added. However, the value of one pointer variable can be subtracted from the other pointer variable provided both pointer point to same variable.

    两个指针变量不能相乘或相加。 但是,如果两个指针都指向同一个变量,则可以从另一个指针变量中减去一个指针变量的值。

递增或递减指针变量 (Incrementing or Decrementing Pointer Variable)

int a,*p1;
p1 = &a;
p1++; //value increased by 2 bytes
p1--; //value decreased by 2 bytes

char b,*p2;
p2 = &b;
p2++; // value decreased by 1 bytes
p2--; // value decreased by 1 bytes

float c,*p3;
p3 = &c;
p3++; // value decreased by 4 bytes
p3- -; // value decreased by 4 bytes

指针和数组 (Pointers and Arrays)

Each element of an array can be accessed easily and efficiently by using pointers.

使用指针可以轻松高效地访问数组的每个元素。

For example, “int a[5]” shows that the variable a is an array that holds 5 elements of integer data type. By using pointer arithmetic, we can access each array element conveniently even in large and complex programs.

例如,“ int a [5]”表明变量a是一个包含5个整数数据类型元素的数组。 通过使用指针算法,即使在大型复杂程序中,我们也可以方便地访问每个数组元素。

As discussed earlier, a pointer variable holds the very first memory address which is known as base address. In case of arrays, the name of the variable is itself base address. In fact, this base address is a pointer itself that points to the first element of array (at subscript 0).

如前所述,指针变量保存着第一个存储地址,即基地址。 对于数组,变量名本身就是基地址。 实际上,此基址本身就是一个指针,它指向数组的第一个元素(在下标0处)。

int a[5];

Here, a is an array variable of integer data type as well as it stores the very first memory address known as a base address.

在此,a是整数数据类型的数组变量,并且它存储了称为基地址的第一个内存地址。

Now each array element can be accessed by using this base address and the incremented by 1 successively for accessing the next array element.

现在,可以使用此基址访问每个数组元素,并依次递增1以访问下一个数组元素。

指针和常数 (Pointers and Constants)

Pointers can also point to a constant. It specifies that a pointer can read the value of a variable to which it points but cannot modify the value of that variable.

指针也可以指向常量。 它指定一个指针可以读取其指向的变量的值,但不能修改该变量的值。

int x;
int y=20;
const int*ptr = &y; // pointer to integer constant
x = *ptr;           // valid. Reading ptr
*ptr = x;           // error! modifying ptr, which is a constant qualified

这个指针 (This Pointer)

‘this’ pointer is an internal pointer that holds the memory address of a current object. ‘this’ pointer is a variable that is used to access the address of the class itself. This unique pointer is automatically passed to a member function when it is called. The pointer ‘this’ acts as an implicit argument to all the member function. ‘this’ keyword is used to represent an object that invokes the member function. ‘this’ pointer is very important when operators are overloaded.

“ this”指针是一个内部指针,用于保存当前对象的内存地址。 “ this”指针是一个变量,用于访问类本身的地址。 该唯一指针在调用时会自动传递给成员函数。 指针“ this”充当所有成员函数的隐式参数。 “ this”关键字用于表示调用成员函数的对象。 当运算符超载时,“ this”指针非常重要。

翻译自: https://www.journaldev.com/30481/pointers-in-c-and-c-plus-plus

c语言指针++

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值