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>.
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>.
本文介绍了计算机内存中变量的存储方式及其地址的概念,并详细讲解了指针的基本使用方法及两个关键操作符。此外,还探讨了数组的概念及其与指针之间的联系,并对C风格字符串进行了说明,包括其表示形式及不足之处。
6660

被折叠的 条评论
为什么被折叠?



