`
java-mans
  • 浏览: 11417686 次
文章分类
社区版块
存档分类
最新评论

指针,数组和串

 
阅读更多
1.Pointers
Each program variable is stored in the computer's memory at some location, or address.
A pointer is a variable that holds the value of such an address.
For example:
Given a type T, the type T* denotes a pointer to a variable of type T.


Two essential operators are used ti manipulate pointers.
The first returns the address of an object in memory,
and the second returns the contents of a given address.


2.Arrays
An arrays is a collection of elements of same type.
Given any type T and a constant N, a variable of type T[N] holds an array of N elements,
each of type T.Each element of the array is referenced by its index, that is, a number
from 0 to N-1.


3.Pointers and Arrays
The name of an array is equivalent to a pointer to the array's initial element and vice versa.
for example:
char c[] = {'c', 'a', 't'};
char* p = c; // p points to c[0]
char* q = &c[0]; // q also points to c[0]
cout << c[2] << p[2] << q[2]; // outputs "ttt"


4. Strings
A string literal such as "Hello, World", is represented as a fixed-length array of characters
that ends with the null character. Character strings represented in this way are called C-stype
strings. since they are inherited from C, unfortunately, this representation alone does not
provide many string operations, such as concatenation and comparison. It also possesses all the
peculiarities of C++ arrays, as mentioned earlier.
For this reason, C++ provides a string type as part of its Standard Template Library(STL). When
we need to distinguish, we call these STL strings. In order to use STL strings it is necessary
to include the header file <string>.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics