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.
我们知道指针是⼀个整数值的地址,可以像对数字值⼀样对指针执⾏各种算术运算。
variable used in lambda
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”指针⾮常重要。
c语⾔指针++

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。