1、赋值

同类指针变量可以相互赋值;数组只能一个元素一个元素地赋值或者拷贝;

2、存储方式

数组:存储空间在静态区或者是栈上,是一段连续的内存;但是数组名不需要单独的存储空间,因为它是一个地址常量

指针:指针的存储空间不确定

3、sizeof

数组占用空间大小:sizeof(数组名);数组元素个数:sizeof(数组名)/sizeof(数据类型)

32位平台下,指针大小为4字节;64位平台下,指针大小是8字节;

4、函数传参

数组传递参数时,会退化为指针(C语言以值拷贝的方式传递参数),将数组名看成常量指针,传递数组首元素的地址

5、数组名可以作为指针常量

#include <stdio.h>

int main(void)
{
	int arry[5] = {1, 2, 3, 4, 5};
	array++;
}

上述代码会报错,报错原因数组名array数组首地址,是一个指针常量,不是变量, 不能对数组名进行自增操作,因为数组名本身是不可修改的。

① 数组首元素自增:

#include <stdio.h>

int main(void)
{
    int array[5] = {1, 3, 5, 7, 9};
    array[0]++;  
    printf("array[0] = %d\n", array[0]);  
    return 0;
}
PS D:\Code\Data Structure> gcc .\test.c -o test
PS D:\Code\Data Structure> .\test.exe
array[0] = 2

②  指针指向第二个数组元素:

#include <stdio.h>

int main(void)
{
    int array[5] = {1, 3, 5, 7, 9};
    int* ptr = array;  // ptr 是一个指向数组首元素的指针

    ptr++;  // 指针自增,指向下一个元素
    printf("ptr points to: %d\n", *ptr);  // 输出指针指向的值
    return 0;
}
PS D:\Code\Data Structure> gcc .\test.c -o test
PS D:\Code\Data Structure> .\test.exe
ptr points to: 3

数组名虽然不能进行自增,但是可以用于运算

#include <stdio.h>

int main(void)
{
    int array[5] = {1, 3, 5, 7, 9};
    printf("The second element's ptr: %p\n", array + 1); 
    return 0;
}
PS D:\Code\Data Structure> gcc .\test.c -o test
PS D:\Code\Data Structure> .\test.exe
The second element's ptr: 0061FF10

 

posted on 2025-02-27 21:44  轩~邈  阅读(71)  评论(0)    收藏  举报