C/C++ 배열과 포인터
배열과 포인터
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int a[5] = { 1,2,3,4,5 };
int* p = a; //or &a[0]
for (int i = 0; i < 5; i++)
{
printf("p 주소: %d, a[%d] 주소: %d\n", p + i, i , &a[i]);
printf("p 값: %d, a[%d] 값: %d\n", *(p + i),i ,a[i]);
}
return 0;
}
a[0]은 배열 a의 베이스 주소입니다. + 1을 하면 4바이트를 넘어서 다음 주소인 a[1]에 접근하게 됩니다.
코드를 보면 알 수 있듯이 *(p + i)대신에 *(a + i)를 사용할 수도 있습니다.
Leave a comment