#include <stdio.h>

int main() {
    // 포인터 : 주소를 저장하는 변수
    int a = 10; 
    int *p = &a; // a의 주소를 p에 저장한다
    printf("a = %d\n", a);
    printf("a의 주소 = %p\n", p);

    // 포인터 변수 선언
    // 자료형 * 변수명;
    int *b;

    // 변수 앞에 &를 붙이면 변수의 주소를 의미한다
    b = &a;

    *b = 20;
    printf("%d\n", a);

    int x = 3;
    int y = 7;
    int *ptr = &x;
    
    *ptr = y;     // (1) x = 7
    ptr = &y;     // (2) ptr 변수에 y의 주소를 저장한다
    *ptr = 10;    // (3) y = 10


    printf("x = %d\n", x);
    printf("y = %d\n", y);

    printf("----문제----\n");
    int num1 = 8, num2 = 15;
    int *ptr1 = &num1;
    int *ptr2 = &num2;

    *ptr1 = *ptr2;   // (1) num1 = 15
    ptr2 = ptr1;     // (2) ptr2가 num1을 가리킴
    *ptr2 = 50;      // (3) num1 = 50

    printf("num1 = %d\n", num1);
    printf("num2 = %d\n", num2);

    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: