Skip to content

🖥️ Great Ideas in Computer Architecture: C Arrays, Strings, & Pointers

约 312 个字 45 行代码 1 张图片 预计阅读时间 2 分钟 共被读过

Instructor: Jenny Song
CS61C su20 - Lecture 3 | 6/26/2020


📚 Review of Last Lecture

  • C Basics
  • Variables, Functions, Control Flow, Syntax.
  • Only 0 and NULL evaluate to FALSE.
  • Pointers
  • Hold memory addresses (address vs. value).
  • Enable efficient code but are error-prone.
  • Pass by Value
  • C functions pass arguments by value; pointers circumvent this.

🏗️ Struct Clarification

Struct Definition

C
struct foo { /* fields */ };  
struct foo name1;         // Declare variable of type struct foo  
struct foo* name2;        // Pointer to struct foo  

Typedef with Struct

C
// Method 1  
struct foo { /* fields */ };  
typedef struct foo bar;  
bar name1;  

// Method 2 (combine definition and typedef)  
typedef struct foo { /* fields */ } bar;  
bar name1;  

🌟 Great Idea #1: Levels of Representation/Interpretation


📜 Agenda

  1. C Operators
  2. Arrays
  3. Strings
  4. More Pointers (Arithmetic, Misc)

🔢 C Operators

Operator Precedence Table

Precedence Operator Description Associativity
1 ++, -- (post) Postfix increment/decrement Left-to-right
1 () Function call
1 [] Array subscripting
2 ++, -- (pre) Prefix increment/decrement Right-to-left
2 *, & Dereference, Address-of

Common Pitfalls

  • Assignment vs. Equality
    C
    a = b;   // Assignment  
    a == b;  // Equality test  
    
  • Operator Binding
  • -x & 1 == 0x & (1 == 0) (not (x & 1) == 0).

📦 Arrays

Basics

C
int ar;              // Declare 2-element array  
int ar[] = {795, 635};  // Declare and initialize  

- Pitfalls: No bounds checking! Accessing ar[n] where n >= 2 causes undefined behavior.

Arrays vs. Pointers

  • Similarities:
    C
    char* buffer;  // Pointer  
    char buffer[]; // Array (read-only pointer)  
    
  • Differences:
  • sizeof(ar) returns array size; sizeof(ptr) returns pointer size.
  • Arrays cannot be reassigned (ar = new_array is invalid).

Example: Zeroing an Array

C
// Method 1: Array notation  
for (i = 0; i < SIZE; i++) ar[i] = 0;  

// Method 2: Pointer arithmetic  
for (i = 0; i < SIZE; i++) *(ar + i) = 0;  

// Method 3: Pointer traversal  
for (int* p = ar; p < ar + SIZE; p++) *p = 0;  

📜 Strings in C

  • Definition: Null-terminated char array.
    C
    char s[] = "abc";  // Equivalent to {'a', 'b', 'c', '\0'}  
    
  • Common Functions (#include <string.h>):
  • strlen(s): Returns length (excluding \0).
  • strcmp(s1, s2): Returns 0 if equal.
  • strcpy(dest, src): Copies src to dest.

Example

C
char s1, s2;  
strcpy(s1, "hi");  
strcpy(s2, "hi");  

- strcmp(s1, s2) == 01 (true).
- s1 == s20 (compares addresses, not content).

🎯 Pointers

Pointer Arithmetic

  • Rules:
  • ptr + n adds n * sizeof(*ptr) to the address.
  • Valid operations: ptr ± int, subtract pointers, compare pointers.
  • Example:
    C
    int A[] = {5, 10};  
    int* p = A;  
    p++;  // Moves to A (address += sizeof(int))  
    

Pointers to Pointers

C
void IncrementPtr(int** h) { *h = *h + 1; }  
int A[] = {50, 60, 70};  
int* q = A;  
IncrementPtr(&q);  // q now points to A  

🧩 Struct Alignment

  • Rules:
  • Members aligned to their size (e.g., int aligned to 4 bytes).
  • Padding added to meet alignment requirements.

Example

C
struct hello {  
  int a;     // 4 bytes  
  char b;    // 1 byte (+3 padding)  
  short c;   // 2 bytes  
  char* d;   // 4 bytes  
  char e;    // 1 byte (+3 padding)  
};  
// Total size: 4 + (1+3) + 2 + 4 + (1+3) = 16 bytes  

🚨 Common Pitfalls & Tips

  1. Uninitialized Pointers:
    C
    int* ptr;  // Points to garbage! Always initialize.  
    
  2. Array Decay: When passed to functions, arrays decay to pointers (losing size info).
  3. Null Terminator: Forgot \0 in strings? strlen may read garbage!

Key Takeaways:
- Arrays and pointers are powerful but error-prone.
- Always manage memory carefully and use sizeof() for portability.
- Understand alignment to optimize struct layouts!