DEV Community

San Kang
San Kang

Posted on

[Adult Learning Log] Data Structures – Week 2 Review

○ Key Takeaways from Week 2

  • Learned the concept of arrays and array-based sequential lists.
  • Studied 1D/2D array declarations, initialization, sparse matrix representation, and transpose matrices.
  • Learned how strings are implemented and the role and danger of omitting the \0 null character.
  • Understood how arrays behave when passed as function arguments.
  • Covered structure definitions, declarations, operations, and nested structures.
  • Learned about pointers and dynamic memory allocation.

○ Sequential List

  • A list implemented using arrays.
  • An array is a collection of elements of the same data type.
  • Stored sequentially in memory, both logically and physically (addresses are also consecutive).
  • Offers fast access via index but can be slow for insert/delete operations.
  • Important to understand the abstract data type (ADT) concept of a list and how to implement it using arrays.
  • Later, this will be compared with linked lists (pointer-based).

○ 1D Array

  • A set of pairs. Accessing by index yields the corresponding value.
  • Declared as data_type array_name[size];
    • int A[6];
    • int A[6] = {1, 2, 3, 4, 5, 6};
    • int A[] = {1, 2, 3, 4, 5, 6};
  • A holds the address of A[0].
  • Local variables in C are not auto-initialized to 0, so uninitialized values may contain garbage data.

○ 2D Array

  • Declared as data_type array_name[rows][columns];
    • int A[2][3]; creates a 2x3 matrix.
  • Initialized like:
    • int A[2][3] = {{1,2,3}, {4,5,6}};
    • int A[2][3] = {1,2,3,4,5,6};
  • Sparse matrices save space but make operations more complex.
  • Represented as triplets: {row, column, value}
  • Transpose matrix: swaps rows and columns, useful when column-wise operations are needed.

○ Strings = Character Array + Null Terminator \0

  • Example: char S1[16] = "Hello World"; automatically adds \0 at the end.
  • Without \0, memory is read until a null byte is found—may cause garbage output or segmentation fault.
  • Use strcpy() or strcpy_s() to initialize character arrays safely.

○ Passing Arrays to Functions

  • C uses call-by-value, but arrays are passed by reference (by address).
  • Changes inside the function affect the original array.
  • Must also pass the size of the array explicitly.

○ Structure (struct) Basics

  • Combines different data types under one unit.
  • Must be defined before use.
typedef struct { int id; char name[20]; double score; } Student; Student a = {202403156, "Hong Gil-dong", 96.3}; a.id = 242403156; strcpy(a.name, "Hong Gil-dong"); 
Enter fullscreen mode Exit fullscreen mode
  • Use . to access members.
  • strcpy() is needed for character arrays (strings).

○ Struct Operations and Nested Structures

  • Only assignment (=) is supported. Other operations like comparison (>) are not allowed.
typedef struct { int month; int date; } Birthday; typedef struct { char name[20]; Birthday birthday; } Friend; Friend list[45]; 
Enter fullscreen mode Exit fullscreen mode

○ Passing Structs to Functions

  • Passed by value (copied), so changes inside the function don’t affect the original.
  • Use pointers to modify original struct values.

○ Polynomial Representation

  • Practice in storing and manipulating structured data.
  • Store coefficients in arrays, usually in ascending order by degree:
    • int coef[] = {7, 5, 2, 3}; → index 0 = constant, index 3 = highest degree term.
    • Easier coding and readability when using index = degree.

○ Pointer Basics

  • A pointer holds the address of another variable.
  • Use * to access or modify the value being pointed to.
  • Use & to get a variable's address.
  • Arrays act like pointers in many contexts.

○ Dynamic Memory Allocation

  • Allocates memory at runtime, allows efficient use of memory.
  • malloc() is used to request memory from the heap.
  • Always free() the memory to avoid memory leaks.
int *pi; pi = (int*)malloc(sizeof(int)); // use pi free(pi); 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)