C++ Learning (2)
2016-07-21 19:21
323 查看
Strings
2 types are supported.- C style char string (the last char will automatically be ‘\0’)
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; char greeting[] = "Hello";
C++ library String
Pointers
* is not *
Note that the asterisk (*) used when declaring a pointer only means that it is a pointer, and should not be confused with the dereference operator.myvar = 25; foo = &myvar; // address 0f //These are ture: myvar == 25; &myvar == 1776; foo == 1776; *foo == 25;
A program test if you understand.
// my first pointer #include <iostream> using namespace std; int main () { int firstvalue, secondvalue; int * mypointer; mypointer = &firstvalue; *mypointer = 10; mypointer = &secondvalue; *mypointer = 20; cout << "firstvalue is " << firstvalue << '\n'; cout << "secondvalue is " << secondvalue << '\n'; return 0; /* OUTPUT: firstvalue is 10 secondvalue is 20 */
Here is a even more elaborate example:
#include <iostream> using namespace std; int main () { int firstvalue = 5, secondvalue = 15; int * p1, * p2; p1 = &firstvalue; p2 = &secondvalue; *p1 = 10; *p2 = *p1; p1 = p2; *p1 = 20; cout << "firstvalue is " << firstvalue << '\n'; cout << "secondvalue is " << secondvalue << '\n'; return 0; }
int *ip; // pointer to an integer double *dp; // pointer to a double float *fp; // pointer to a float char *ch // pointer to character
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the sam**e, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer **points to.
Important operations
(a) we define a pointer variables. (b) assign the address of a variable to a pointer (c) finally access the value at the address available in the pointer variable.NULL pointer:
It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned.int *ptr = NULL;
Pointer arithmetic
There are four arithmetic operators that can be used on pointers: ++, –, +, and –.let us consider that ptr is an integer pointer which points to the address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation on the pointer: ptr++, the ptr will point to the location 1004 because each time ptr is incremented, it will point to the next integer. If ptr points to a character whose address is 1000, then above operation will point to the location 1001 because next character will be available at 1001.
Incrementing a Pointer example:
#include <iostream> using namespace std; const int MAX = 3; int main (){ int var[MAX] = {10, 100, 200}; int *ptr; // let us have array address in pointer. ptr = var; for (int i = 0; i < MAX; i++){ cout << "Address of var[" << i << "] = "; cout << ptr << endl; cout << "Value of var[" << i << "] = "; cout << *ptr << endl; // point to the next location ptr++; } return 0; }
output on 32bit machine
Address of var[0] = 0xbfa088b0 Value of var[0] = 10 Address of var[1] = 0xbfa088b4 Value of var[1] = 100 Address of var[2] = 0xbfa088b8 Value of var[2] = 200
Decrementing a Pointer: ptr = &var[MAX-1];
#include <iostream> using namespace std; const int MAX = 3; int main (){ int var[MAX] = {10, 100, 200}; int *ptr; // let us have address of the last element in pointer. ptr = &var[MAX-1]; for (int i = MAX; i > 0; i--){ cout << "Address of var[" << i << "] = "; cout << ptr << endl; cout << "Value of var[" << i << "] = "; cout << *ptr << endl; // point to the previous location ptr--; } return 0; }
Pointer Comparisons
Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and p2 point to variables that are related to each other, such as elements of the same array, then p1 and p2 can be meaningfully compared.Pointer and Array
int var[MAX] = {10, 100, 200}; *var = i; // this is equivalent to var[0] = i var++; /* This is incorrect because var is a constant that points to the beginning of an array. */
Because an array name generates a pointer constant, it can still be used in pointer-style expressions, as long as it is not modified. *(var + 2) = 500; is equal to var[2] = 500;
Array of pointers
#include <iostream> using namespace std; const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int *ptr[MAX]; // array of integer pointers for (int i = 0; i < MAX; i++) { ptr[i] = &var[i]; // assign the address of integer. } for (int i = 0; i < MAX; i++) { cout << "Value of var[" << i << "] = "; cout << *ptr[i] << endl; // use pointer to output. } return 0; }
Use an array of pointers to character to store a list of strings.
#include <iostream> using namespace std; const int MAX = 4; int main () { char *names[MAX] = { "Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali", }; for (int i = 0; i < MAX; i++) { cout << "Value of names[" << i << "] = "; cout << names[i] << endl; } return 0; }
Pointer’s pointer (Multiple Direction)
A variable that is a pointer to a pointer must be declared as such. This is done by placing an additional asterisk in front of its name. int **var;#include <iostream> using namespace std; int main () { int var; int *ptr; int **pptr; var = 3000; // take the address of var ptr = &var; // take the address of ptr using address of operator & pptr = &ptr; // take the value using pptr cout << "Value of var :" << var << endl; cout << "Value available at *ptr :" << *ptr << endl; cout << "Value available at **pptr :" << **pptr << endl; return 0; }
passing pointer to function
int main () { unsigned long sec; getSeconds( &sec ); //passing address to function. // print the actual value cout << "Number of seconds :" << sec << endl; return 0; } void getSeconds(unsigned long *par){ //... }
相关文章推荐
- 关于C变量作用域和生存期的常见问题
- HDU - 4544 湫湫系列故事——消灭兔子 贪心+优先队列
- Subsets
- Multiply Strings
- C++中结构体和类
- Leetcode 191. Number of 1 Bits (Easy) (cpp)
- C/C++笔记--strcpy和strncpy函数的安全性分析
- Leetcode 342. Power of Four (Easy) (cpp)
- poj2632 Crashing Robots
- PAT乙级1001. 害死人不偿命的(3n+1)猜想 (15) C++
- c语言的一些语句说明和查找问题
- [算法学习笔记]冒泡排序和选择排序
- c++学习笔记(十一):C++类的访问修饰符
- Windows下php扩展开发c++动态库
- C++--单例模式样板
- 高质量C和C++编程 读书笔记 (第一二章)
- C/C++面试题:static(静态)变量的作用
- C语言求矩阵偶数的平均值和程序输出某数的问题
- 《深度探索C++对象模型》2
- C++ STL Adaptor stack、queue和vector的使用