C 怎么生命int数组的指针
时间: 2023-07-20 09:09:39 浏览: 51
可以通过两种方式声明 int 数组的指针,如下所示:
1. 使用数组名作为指针变量名:
```c
int arr[10];
int *ptr = arr; // ptr指向arr数组的第一个元素
```
2. 使用 malloc 函数动态分配数组空间:
```c
int *ptr = (int*)malloc(10 * sizeof(int)); // 分配一个包含10个元素的int数组空间
```
在这两种情况下,ptr 都是指向 int 数组的指针。可以通过使用指针访问数组中的元素,如下所示:
```c
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr = arr;
// 访问数组中的元素
for (int i = 0; i < 10; i++) {
printf("%d ", *(ptr+i));
}
```
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)