Computer Science

C Program — Structs, Scope & Algorithm

2023-08-13 2 min read

Struct

A structure is a derived (composite) data type that groups several attributes into one type; those attributes can be primitive types or other composite types.

Declaring a struct

Accessing struct members

Nested structs

Arrays of structs

Accessing members through a struct pointer

Scope

Scope is the region where a variable can be used — usually the { } block it is declared in.

Example: a custom function and main are two separate { } blocks, so reusing the same variable name in both causes no error — to the program they are two different scopes.

Example: a variable defined inside a while/for loop in main lives only within that loop’s { } and cannot be used outside it.

Argument Passing

Call by Value

With call by value, the a/b in main and the a/b inside the swap function live in separate memory, so they never interfere with each other — but it needs two extra memory slots to hold the copies.

Call by Address

The swap function swaps the values at the addresses passed in. In main we pass the addresses with the & operator; after one run you can see that before the swap a = 1 and b = 0, and after swapping through their memory addresses their values become 0 and 1 respectively.

Algorithm

An algorithm is a finite set of steps that solves a specific problem. In essence, it’s just a logical way of thinking through a problem.

Example: say the specific problem is “turn apples into a glass of apple juice.”

You could do it with these steps:

  1. Wash the apples.
  2. Peel and core the apples.
  3. Put the prepared apples into the blender.
  4. Add a suitable amount of water.
  5. Press the blender’s start button.
  6. Pour the juice into a glass.

← Posts
meow~