使用指针星号转移c语言,C中的指针:何时使用号和星号?
你有指針和值:
int* p; // variable p is pointer to integer type
int i; // integer value
將指針轉(zhuǎn)換為帶*的值:
int i2 = *p; // integer i2 is assigned with integer value that pointer p is pointing to
您可以將值轉(zhuǎn)換為帶& ;:的指針
int* p2 = &i; // pointer p2 will point to the address of integer i
編輯:
在數(shù)組的情況下,它們非常類似于指針。如果你認(rèn)為它們是指針,你將使用*來獲取其中的值,如上所述,但是還有另一個(gè)更常見的方法使用[]運(yùn)算符:
int a[2]; // array of integers
int i = *a; // the value of the first element of a
int i2 = a[0]; // another way to get the first element
獲取第二個(gè)元素:
int a[2]; // array
int i = *(a + 1); // the value of the second element
int i2 = a[1]; // the value of the second element
所以[]索引操作符是一個(gè)特殊形式的*運(yùn)算符,它的工作原理如下:
a[i] == *(a + i); // these two statements are the same thing
總結(jié)
以上是生活随笔為你收集整理的使用指针星号转移c语言,C中的指针:何时使用号和星号?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 修改表字段长度的操作,对业务是否有影响?
- 下一篇: 折半查找判定树及平均查找长度