char*[]怎么转char**
时间: 2024-07-14 16:00:36 浏览: 175
`char*[]` 是一个字符指针数组,它存储了多个指向字符的指针。要将 `char*[]` 转换成 `char**`,即一个指向字符指针的指针数组,你可以按照以下步骤操作:
1. 声明一个新的 `char**` 类型的指针变量,用于存放转换后的结果。
2. 使用循环遍历 `char*[]` 数组,为新创建的 `char**` 变量分配内存并复制每个元素的地址。
示例代码如下(假设有一个 `char*[]` 变量叫 `strArray`):
```c
char* strArray[] = {"Hello", "World", "CSDN"};
int len = sizeof(strArray) / sizeof(strArray); // 获取数组长度
char** strPtrArray = (char**)malloc(len * sizeof(char*)); // 分配内存
if (strPtrArray != NULL) {
for (int i = 0; i < len; i++) {
strPtrArray[i] = strArray[i]; // 复制地址
}
}
```
完成后,`strPtrArray` 就是一个指向 `char*` 的指针数组,其中每个元素都对应 `strArray` 中的一个字符串指针。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)