# 笔记：C (C++)语言中数组与指针的区别

-关于《c专家编程》的读书笔记

—————————————————

void funcA(int* p)
{
printf(“%d”, p[3]);    printf(“%d”, p[2]);
}

void funcB(int arr[])
{
printf(“%d”, arr[3]);    printf(“%d”, arr[2]);
}

int _tmain(int argc, _TCHAR* argv[])
{
int arrayInt[5] = {3, 4, 5, 6, 7};
int* pInt = arrayInt;

printf(“%d”, arrayInt[3]);
printf(“%d”, pInt[3]);

funcA(arrayInt);    funcB(arrayInt);

funcA(pInt);    funcB(pInt);

return 0;
}

; 23   :     int arrayInt[5] = {3, 4, 5, 6, 7};
mov    DWORD PTR _arrayInt\$[ebp], 3
mov    DWORD PTR _arrayInt\$[ebp+4], 4
mov    DWORD PTR _arrayInt\$[ebp+8], 5
mov    DWORD PTR _arrayInt\$[ebp+12], 6
mov    DWORD PTR _arrayInt\$[ebp+16], 7

; 24   :     int* pInt = arrayInt;
lea    eax, DWORD PTR _arrayInt\$[ebp]
mov    DWORD PTR _pInt\$[ebp], eax

pInt指向arrayInt的首地址_arrayInt\$[ebp]，将其保存在_pInt\$[ebp]

mov    eax, DWORD PTR _arrayInt\$[ebp+12]

mov    eax, DWORD PTR _pInt\$[ebp]
mov    ecx, DWORD PTR [eax+12]

; 8    :     printf(“%d”, p[3]);
mov    eax, DWORD PTR _p\$[ebp]
mov    ecx, DWORD PTR [eax+12]

; 14   :     printf(“%d”, arr[3]);
mov    eax, DWORD PTR _arr\$[ebp]
mov    ecx, DWORD PTR [eax+12]

—————————————————

—————————————————

CONST    SEGMENT
OBNGGKAB DB ‘This is array string’, 00H ; `string’
……
CONST    ENDS

; 23   :     char* pStr1 = _T(“This is array string”);
mov    DWORD PTR _pStr1\$[ebp], OFFSET OBNGGKAB

; 24   :     char arrayString[] = _T(“This is array string”);
mov    eax, DWORD PTR OBNGGKAB
mov    DWORD PTR _arrayString\$[ebp], eax
mov    ecx, DWORD PTR OBNGGKAB+4
mov    DWORD PTR _arrayString\$[ebp+4], ecx
mov    edx, DWORD PTR OBNGGKAB+8
mov    DWORD PTR _arrayString\$[ebp+8], edx
mov    eax, DWORD PTR OBNGGKAB+12
mov    DWORD PTR _arrayString\$[ebp+12], eax
mov    ecx, DWORD PTR OBNGGKAB+16
mov    DWORD PTR _arrayString\$[ebp+16], ecx
mov    dl, BYTE PTR OBNGGKAB+20
mov    BYTE PTR _arrayString\$[ebp+20], dl

pStr1[1] = ‘H’; // critical error

Unhandled exception at 0x00411594 in testbbbb1.exe: 0xC0000005: Access violation writing location 0x004157c1.