https://www.youtube.com/watch?v=RqP_Q92wUSw&list=PLgqG2uj21HgkcfVtlr5rPekQl5VWJEnIB&index=14

학습 목표

  1. 포인터와 배열/문자열
  2. 포인터와 구조체

포인터와 배열/문자

포인터에 +1 연산을 한다면 자료형만큼의 바이트값이 증가하게 된다. 따라서 배열과 같은경우 아래의 예시처럼 다음값을 출력하게 된다.

#include <iostream>

using namespace std;
int main(){
    double* p = new double[3];
    p[0] = 1.0;
    p[1] = 2.0;
    p[2] = 3.0;
    
    cout << p[1] << endl;

    p = p + 1;

    cout << p[0] << endl;

    delete[] p;

    return 0;
}

포인터와 구조체

구조체에서 멤버에 접근하는 연산은 .으로 지정했지만 포인터 변수에서 구조체에 접근을 하여면 - >을 통해서 접근할 수 있다. (*포인터 변수)를 이용하면 그전에 배웠던 멤버에 접근하는 연산자인 .을 사용할 수 있다.

#include <iostream>

struct MyStruct
{
    char name[20];
    int age;
};

int main(){
    using namespace std;
    
    MyStruct* temp = new MyStruct;
    cout << "what is your name?\\n";
    cin >> temp->name;

    cout << "what is your age?\\n";
    cin >> (*temp).age;

    cout << "Hello ,"<< temp->name << endl;
    cout << "Your age is ,"<< temp->age << endl;

    return 0;
}