📚 CS61C Lecture Notes: Introduction to C & Pointers¶
约 282 个字 34 行代码 预计阅读时间 2 分钟 共被读过 次
Instructor: Stephan Kaminsky | Date: Jun 2019
📌 Key Concepts in C Programming¶
1. Compilation in C¶
- Compiled Language: Converts C code directly to machine-specific instructions (0s and 1s).
- 🚀 Advantages: Faster execution than Java/Python (no bytecode/JVM).
- ⚠️ Disadvantages: Platform-dependent executables; slower edit-compile-run cycle.
2. Variable Types & Declarations¶
- Typed Variables: Must declare type before use.
- Example:
- Type Sizes: Machine-dependent (e.g.,
int
= 4/8 bytes). - Special Keywords:
short
,long
,unsigned
.
3. Characters & ASCII¶
- ASCII Encoding: Characters stored as numbers (e.g.,
'a'
= 97).
- Size: 1 byte (8 bits).
4. Type Casting¶
- Weak Typing: Explicitly cast between types.
- Example:
- ⚠️ Caution: Risky casts (e.g., casting structs to integers).
5. Functions¶
- Prototypes & Definitions:
- Return Types: Must declare return type (
void
for no return).
6. Structs & Unions¶
- Structs: Group related variables.
- Unions: Overlapping memory for different types.
🧮 Example: Struct Padding¶
C
struct foo {
int a; // 4 bytes
char b; // 1 byte (+3 padding)
struct foo* c; // 4 bytes
};
// Total size = 12 bytes (32-bit architecture)
📊 C vs Java Comparison¶
Feature | C | Java |
---|---|---|
Language Type | Function-Oriented | Object-Oriented |
Memory Management | Manual (malloc , free ) | Automatic (Garbage Collection) |
Hello World | printf("Hello\n"); | System.out.println(...); |
🎯 Pointers: Address vs Value¶
- Pointer Syntax:
- Pointer Types:
int*
,char*
,void*
(generic pointer).- ⚠️ Dangling Pointers: Uninitialized pointers → undefined behavior!
🔍 Example: Pointer Parameter Passing¶
❓ Quiz: 4-Bit Number Representations¶
Given x = 0b1010
(4 bits), which value does NOT represent x
?
Options:
(A) -4 (B) -6 (C) 10 (D) -2
Analysis:
- Unsigned: \(2^3 + 2^1 = 10\) ✔️ (C)
- Sign & Magnitude: \(-2^1 = -2\) ✔️ (D)
- Biased (Bias=7): \(10 - 7 = 3\) → Not an option.
- Two’s Complement: \(-6\) ✔️ (B)
- One’s Complement: \(-5\) → Not an option.
Answer: (A) -4 ❌
🛑 Common Pointer Bugs¶
- Uninitialized Pointers:
- Memory Leaks: Forgetting to
free()
aftermalloc()
.
📖 Key Takeaways¶
- C offers low-level control but requires careful memory management.
- Pointers = powerful but error-prone. Always initialize!
- Structs/unions organize data; padding affects memory layout.
🔗 Resources:
- K&R Book ("The C Programming Language")
- C99 Standard