--- id: c-pointers-arithmetic title: "C Pointer Arithmetic" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["pointer increment", "pointer subtraction", "type-scaled pointer movement", "C 포인터 연산"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.87 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["c", "programming-language", "w3schools", "pointers", "pointer-arithmetic"] raw_sources: ["https://www.w3schools.com/c/c_pointers_arithmetic.php"] applied_in: [] github_commit: "" --- # [[C Pointer Arithmetic]] ## 🎯 한 줄 통찰 (One-line insight) Adding `1` to a pointer never means "move forward by 1 byte" — it means "move forward by the SIZE of whatever type the pointer points to" — so from the same starting address, `int* + 1` jumps 4 bytes while `char* + 1` jumps only 1 byte, meaning pointer arithmetic is silently type-scaled, and mixing up pointer types is explicitly called out as a common mistake that lands you at the wrong memory location entirely. [S1] ## 🧠 핵심 개념 (Core concepts) - **Pointer arithmetic** — changing a pointer's VALUE to make it reference a different element, exploiting the fact that array elements sit contiguously in memory. [S1] - **`++`/`--`/`+=`/`-=` on pointers** — move the pointer forward/backward by one (or N) elements, just like a loop counter. [S1] - **Type-scaled movement** — `p + 1` advances by `sizeof(pointed-to type)` bytes, not literally 1 byte; an `int*` moves 4 bytes per step while a `char*` moves 1 byte per step. [S1] - **Pointer subtraction (distance)** — subtracting two pointers INTO THE SAME ARRAY yields the number of ELEMENTS between them, not bytes; only valid when both pointers reference the same array. [S1] - **Pointer-driven loop** — looping by incrementing the pointer itself (`p++`) instead of indexing (`myNumbers[i]`) needs no separate index variable at all. [S1] ## 📖 세부 내용 (Details) - Accessing array elements via pointer offset: `int myNumbers[4] = {25, 50, 75, 100}; int *p = myNumbers; printf("%d\n", *p); printf("%d\n", *(p + 1)); // 50`. [S1] - Moving a pointer with increment/decrement/step: `p++; // next element p--; // previous element p += 2; // jump 2 elements`. [S1] - Pointer subtraction giving element count: `int *start = &myNumbers[1]; int *end = &myNumbers[4]; printf("%ld\n", end - start); // 3 elements apart`. [S1] - Type-dependent step size proven side by side: `int *pi = myNumbers; // moves by sizeof(int), typically 4 bytes char *pc = letters; // moves by 1 byte`. [S1] - Pointer-only loop (no index variable): `int *p = myNumbers; for (int i = 0; i < 4; i++) { printf("%d\n", *p); p++; }`. [S1] ## ⚖️ 모순 및 업데이트 (Contradictions & updates) - **포인터 이동은 타입에 따라 크기가 다름**: int*는 한 칸 이동 시 보통 4바이트, char*는 1바이트만 이동한다는 점이 동일한 시작 주소에서 직접 비교되어 확인되며, 타입을 혼동하면 잘못된 메모리 위치를 가리키게 된다고 경고됨. [S1] - **배열 경계를 벗어나는 이동 금지**: 배열의 끝을 한 칸 넘어선 위치까지는 포인터 비교용으로만 안전하며, 그 값을 실제로 역참조해서는 안 된다는 점이 명시됨. [S1] ## 🛠️ 적용 사례 (Applied in summary) 현재 발견된 실제 적용 사례가 없습니다 — 인덱스 변수 없이 포인터 자체를 증가시키며 배열을 순회하는 방식이 메모리를 직접 다루는 실전 코드에서 흔히 쓰이는 패턴으로 소개됨. [S1] ## 💻 코드 패턴 (Code patterns) Looping through an array by advancing the pointer itself, with no index variable (C): ```c int myNumbers[4] = {25, 50, 75, 100}; int *p = myNumbers; // start of array for (int i = 0; i < 4; i++) { printf("%d\n", *p); p++; // move to next element } ``` ## ✅ 검증 상태 및 신뢰도 - **상태:** draft - **검증 단계:** conceptual - **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body) - **신뢰 점수:** 0.87 - **중복 검사 결과:** 신규 생성 (New discovery) ## 🔗 지식 그래프 (Knowledge Graph) - **상위/루트:** [[C Tutorial]] - **관련 개념:** [[C Pointers]], [[C Pointers Arrays]] - **참조 맥락:** 포인터 연산 — 포인터와 배열(Pointers Arrays) 챕터로 이어짐. ## 📚 출처 (Sources) - [S1] W3Schools — C Pointer Arithmetic — https://www.w3schools.com/c/c_pointers_arithmetic.php ## 📝 변경 이력 (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Pointer Arithmetic" page (Astra wiki-curation, P-Reinforce v3.1 format).