This question already has answers here:
Is an array name a pointer?
                                
                                    (9个答案)
                                
                        
                        
                            Why do arrays in C decay to pointers?
                                
                                    (3个答案)
                                
                        
                        
                            why is array name a pointer to the first element of the array?
                                
                                    (3个答案)
                                
                        
                        
                            What's the difference between an array and a pointer in C exactly?
                                
                                    (4个答案)
                                
                        
                                2年前关闭。
            
                    
我正在学习c指针。我了解到数组名称本身就是一个指针。

int a[5]={ 1 , 2 , 3 , 4 , 5 };
int *b = &a;


我的理解:
    a [5]-> a包含基址
    考虑一个在0x100的位置
    因此a [5] = 0x100 +(5 * size_of_integer)
    = 0x100 +(5 * 4)
    = 0x100 +(20)
    = 0x120

b = &a; // b = 0x100b[0] = 0x100b[1] = 0x104

a [0]和b [0]有什么区别?
(我是初学者,我可能错了)

最佳答案

aint a[5]数组。 a指向第一个(a[0])元素。 ba的指针。您可以使用以下代码进行检查:

#include <stdio.h>

int main()
{

    int a[5] = {1, 2, 3, 4, 5};
    int *b = &a;
    printf("a %p\n", a);         // (1) address of a array.
    printf("a[0] %p\n", &a[0]);  // (2) address of first element of a array
    printf("b %p\n", b);         // (3) address of b

    printf("b[2] %p\n", &b[2]);    // (4) means [address of b] + 2 * sizeof(int)
    printf("a[2] %p\n", &a[2]);    // (5) third element of array

    return 0;
}


(1)=(2)=(3)和(4)=(5)

还要b[0] = 1不寻址(如您的示例中的0x100)和b[1] = 2

printf("b[0] %d\n", &b[0]);
printf("b[1] %d\n", &b[1]);

09-04 07:26