指针,数组和串

本文介绍了计算机内存中变量的存储方式及其地址的概念,并详细讲解了指针的基本使用方法及两个关键操作符。此外,还探讨了数组的概念及其与指针之间的联系,并对C风格字符串进行了说明,包括其表示形式及不足之处。
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>.
### 指针数组数组指针的区别及使用场景 #### 区别 指针数组数组指针是两种不同的数据结构,尽管它们的名字听起来相似。以下是它们的核心区别: 1. **指针数组** 指针数组是一个数组,其元素都是指针。每个元素存储的是一个地址值,指向其他数据类型(如整数、字符等)。在32位系统中,每个指针占4个字节,因此整个指针数组的大小由数组的长度决定[^2]。 示例代码如下: ```c int *pArray[3]; // 定义了一个包含3个int指针指针数组 ``` 2. **数组指针** 数组指针是一个指针,指向一个数组。它本身只占用指针大小的空间(例如,在32位系统中为4字节),但它可以指向一个更大的数组[^4]。 示例代码如下: ```c int (*pArray)[3]; // 定义了一个指向包含3个int元素的数组指针 ``` #### 使用场景 1. **指针数组的使用场景** - 当需要存储多个动态分配的对象时,指针数组非常有用。例如,如果需要管理多个字符或动态分配的数组,可以使用指针数组。 - 示例:存储多个字符。 ```c char *strArray[3] = {"Hello", "World", "C++"}; // 指针数组,每个元素指向一个字符常量 ``` 2. **数组指针的使用场景** - 当需要操作二维或多维数组时,数组指针特别有用。通过数组指针,可以方便地访问二维数组的行或列。 - 示例:访问二维数组的行。 ```c int arr[2][3] = {{1, 2, 3}, {4, 5, 6}}; int (*p)[3] = arr; // p指向包含3个int元素的数组 printf("%d\n", (*p)[1]); // 输出2 ``` #### 关键点总结 - 指针数组是一个数组,其元素是指针;而数组指针是一个指针,指向一个数组。 - 在定义时需要注意括号的优先级问题。例如,`int *pArray[3]` 是指针数组,而 `int (*pArray)[3]` 是数组指针[^4]。 - 指针数组的大小由数组长度决定,而数组指针的大小仅由指针本身决定。 ```c // 示例:指针数组数组指针的区别 #include <stdio.h> int main() { // 指针数组 int a = 10, b = 20, c = 30; int *pArray[3]; pArray[0] = &a; pArray[1] = &b; pArray[2] = &c; printf("Pointer Array: %d, %d, %d\n", *pArray[0], *pArray[1], *pArray[2]); // 数组指针 int arr[2][3] = {{1, 2, 3}, {4, 5, 6}}; int (*p)[3] = arr; printf("Array Pointer: %d, %d, %d\n", (*p)[0], (*p)[1], (*p)[2]); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值