Pointer and Array--some code fragment
2011-06-23 11:01
513 查看
Dereference Yields an Lvalue
*sp = "goodbye"; // contents of s now changedstring s2 = "some value";
sp = &s2; // sp now points to s2
Subscripts and Pointers
int ia[] = {0,2,4,6,8};int i = ia[0]; // ia points to the first element in ia
int *p = &ia[2]; // ok: p points to the element indexed by 2
int j = p[1]; // ok: p[1] equivalent to *(p + 1),
// p[1] is the same element as ia[3]
int k = p[-2]; // ok: p[-2] is the same element as ia[0]
Using Pointers to Access Array Elements
int ia[] = {0,2,4,6,8};int *ip = ia; // ip points to ia[0]
ip = &ia[4]; // ip points to last element in ia
ip = ia; // ok: ip points to ia[0]
int *ip2 = ip + 4; // ok: ip2 points to ia[4], the last element in ia
// error: ia has only
4000
4 elements, ia + 10 is an invalid address
int *ip3 = ia + 10;
ptrdiff_t n = ip2 - ip; // ok: distance between the pointers
int last = *(ia + 4); // ok: initializes last to 8, the value of ia[4]
last = *ia + 4; // ok: last = 4, equivalent to ia[0]+4
Pointer to pointer
int ival = 1024;int *pi = &ival; // pi points to an int
int **ppi = π // ppi points to a pointer to int
int *pi2 = *ppi; // ppi points to a pointer
cout << "The value of ival/n"
<< "direct value: " << ival << "/n"
<< "indirect value: " << *pi << "/n"
<< "doubly indirect value: " << **ppi
<< endl;
相关文章推荐
- Pointer and Array--some code fragment
- c++ array and pointer
- LintCode "Partition Array by Odd and Even"
- Speeding AnsiStrings tricks, and some code [zz from delphi3000]
- codeforces D. Levko and Array(二分加dp) 挺好的一个题
- use MyButton is simple,only add two file and then add some code just bellow in any dlg src declear the button control and will ok!
- 【Codeforces Round 374 (Div 2)D】【贪心】 Maxim and Array n个数做K次±X使得乘积尽可能小
- pointer to ref of an array, and to the func that return a ref of an array
- JS code do conversion between hexStr and byteArray
- Pointer and Array
- Codeforces Round #179 (Div. 1)-A. Greg and Array
- c pointer and array
- Codeforces Round #249 (Div. 2)C Devu and Partitioning of the Array
- 2D Array and double pointer
- c pointer and array
- Reference To Array And Pointer To Array
- the c programing language chap5 : pointer and array
- Codeforces Round #249 (Div. 2)C Devu and Partitioning of the Array
- Codeforces #179(Div 2)C Greg and Array
- 简析指针与多维数组【转http://tonybai.com/2013/03/28/pointer-and-multi-dimension-array-in-c/】