--- id: c-structs title: "C Structures (structs)" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["struct keyword", "dot syntax", "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", "structs"] raw_sources: ["https://www.w3schools.com/c/c_structs.php"] applied_in: [] github_commit: "" --- # [[C Structures (structs)]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Directly assigning a string literal to a struct's `char[]` member (`s1.myString = "Some text";`) produces a compile ERROR ("assignment to expression with array type") β€” the exact same restriction from the Strings chapter (arrays can't be reassigned wholesale) applies inside structs too, requiring `strcpy()` UNLESS the assignment happens at declaration time using the comma-separated initializer syntax, which is the one place string literals ARE allowed directly. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Structure (`struct`)** β€” groups multiple MIXED-type variables ("members") into one unit, unlike arrays which require a single uniform type. [S1] - **Declaration + variable creation** β€” the `struct Name {...};` block only defines the TEMPLATE; you must separately declare `struct Name variableName;` to create an actual instance. [S1] - **Dot syntax (`.`)** β€” accesses/assigns individual members (`s1.myNum = 13;`). [S1] - **String members require `strcpy()`** β€” direct assignment (`s1.myString = "text";`) fails to compile; use `strcpy(s1.myString, "text");` instead. [S1] - **Initializer-list exception** β€” at DECLARATION time, `struct myStructure s1 = {13, 'B', "Some text"};` allows string literals directly, no `strcpy()` needed β€” values must be listed in the same order as the members were declared. [S1] - **Struct-to-struct copy** β€” `s2 = s1;` copies ALL members at once, including string arrays, without needing `strcpy()` per-field. [S1] - **Template reuse** β€” one struct definition (e.g. `Car`) can be instantiated repeatedly with different values (`car1`, `car2`, `car3`), avoiding separate ad-hoc variable sets per entity. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - The string-assignment error case: `s1.myString = "Some text"; // error: assignment to expression with array type`. [S1] - The `strcpy()` fix: `strcpy(s1.myString, "Some text");`. [S1] - Initializer-list shortcut bypassing `strcpy()`: `struct myStructure s1 = {13, 'B', "Some text"};`. [S1] - Real-life template reuse: `struct Car { char brand[30]; char model[30]; int year; }; struct Car car1 = {"BMW", "X5", 1999}; struct Car car2 = {"Ford", "Mustang", 1969};`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **λ¬Έμžμ—΄ 멀버 직접 λŒ€μž… λΆˆκ°€**: ꡬ쑰체 μ•ˆμ˜ char λ°°μ—΄ 멀버에도 λ°°μ—΄ 챕터와 λ™μΌν•œ μ œμ•½μ΄ μ μš©λ˜μ–΄ strcpy()κ°€ ν•„μš”ν•˜μ§€λ§Œ, μ„ μ–Έ μ‹œ μ΄ˆκΈ°ν™” 리슀트({...}) λ¬Έλ²•μ—μ„œλŠ” μ˜ˆμ™Έμ μœΌλ‘œ λ¬Έμžμ—΄ λ¦¬ν„°λŸ΄μ„ 직접 μ“Έ 수 μžˆλ‹€λŠ” 점이 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) Car ꡬ쑰체 ν•˜λ‚˜λ‘œ BMW/Ford/Toyota μ„Έ λŒ€μ˜ μ„œλ‘œ λ‹€λ₯Έ μžλ™μ°¨ 정보λ₯Ό μ €μž₯ν•˜λŠ” μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨(ν•˜λ‚˜μ˜ ν…œν”Œλ¦Ώ, μ—¬λŸ¬ μΈμŠ€ν„΄μŠ€). [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) One struct template reused for multiple distinct real-world entities (C): ```c struct Car { char brand[30]; char model[30]; int year; }; int main() { struct Car car1 = {"BMW", "X5", 1999}; struct Car car2 = {"Ford", "Mustang", 1969}; printf("%s %s %d\n", car1.brand, car1.model, car1.year); return 0; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.87 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[C Arrays RealLife]], [[C Strings Functions]], [[C Structs Nested]] - **μ°Έμ‘° λ§₯락:** ꡬ쑰체 μ„Ήμ…˜ 첫 챕터 β€” 쀑첩 ꡬ쑰체(Structs Nested) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C Structures (structs) β€” https://www.w3schools.com/c/c_structs.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Structures (structs)" page (Astra wiki-curation, P-Reinforce v3.1 format).