Pointers, Pass by Value vs. Reference, Strings, and File I/O, C 101 – Study Notes
offline

Computer Science I (University of Central Florida)

Tags: pointers, call by value, call by reference, pass by reference, dereference, strings, null terminator, strcmp, strlen, strcpy, FILE pointer, fopen, fclose, string.h

Difficulty: Intermediate | Prerequisites: C Basics (variables, functions, arrays, loops)


Big Picture

Pointers are the concept that separates C from most beginner-friendly languages. They give you direct access to memory addresses, which unlocks pass-by-reference, dynamic memory allocation, and efficient data structures. Strings in C are just arrays of characters ending with a null terminator, so pointer arithmetic applies to them directly. File I/O rounds out this section by showing how your programme reads from and writes to persistent storage. If pointers feel slippery, spend extra time here: nearly every topic that follows (structs, dynamic memory, linked lists) depends on them.


TL;DR

Pointers store memory addresses. Pass-by-value copies data into a function (changes stay local); pass-by-reference passes an address so the function can modify the original. Strings are null-terminated character arrays with their own library (string.h) for copying, comparing, and measuring length. File I/O follows a fixed pattern: declare a FILE *, open with fopen(), check for success, read/write, close with fclose(), and set the pointer to NULL.


Key Terms

Pointer

A variable that stores the memory address of another variable. Declared with (e.g. int p;). Think of it as a signpost: it does not hold the data itself, just the location where the data lives.

Dereference operator (*)

When applied to a pointer, it accesses the value stored at the address the pointer holds. For example, if p points to x, then p gives you the value of x. In simple terms, p means "go to the address stored in p and get the value there."

Address-of operator (&)

Returns the memory address of a variable. &x gives you where x lives in memory. Think of it as asking "what is your address?" rather than "what is your value?"

Call by value

The function receives a copy of the argument. Any changes to the parameter inside the function do not affect the original variable in the caller. In simple terms, the function gets a photocopy, not the original document.

Call by reference (pass by reference)

The function receives the address of the argument (via a pointer). Changes made through that pointer do affect the original variable. Think of it as handing someone your house key instead of a photo of your house.

NULL pointer

A pointer that does not point to any valid memory address. Used as a sentinel to indicate "nothing" or "not initialised." Defined in and . In simple terms, it is a pointer that deliberately points nowhere.

Null character (\0)

The character with ASCII value 0 that marks the end of a C string. Every string literal like "UCF" has an invisible \0 appended after the last visible character. Think of it as the period at the end of a sentence, except invisible.

strlen()

Returns the number of characters in a string, not counting the null terminator. strlen("UCF") returns 3. In simple terms, it measures the visible length of the text.

strcmp()

Compares two strings lexicographically. Returns 0 if equal, a negative value if the first string comes before the second alphabetically, and a positive value if it comes after. Think of it as alphabetical ordering: strcmp("and", "ant") returns a negative value because 'd' comes before 't'.

strcpy()

Copies the contents of one string into another. Defined in . You cannot copy strings with = in C; that only copies pointer addresses, not the characters themselves.

FILE pointer (FILE *)

A pointer to a structure that represents an open file stream. You never access the structure directly; you pass the pointer to functions like fprintf, fscanf, and fclose.


Core Content

Pointers and the * / & Operators

  • int x = 10; int *p = &x; makes p store the address of x

  • *p dereferences p, giving you the value 10

  • (&num) is equivalent to just writing num, because & gets the address and immediately dereferences it back

  • The * symbol serves double duty: in a declaration it means "this is a pointer," and in an expression it means "dereference this pointer"

Call by Value vs. Call by Reference

  • Call by value: the function gets its own copy. Example:

    • void increment(int n) { n++; } — the caller's variable is unchanged after the call

  • Call by reference: the function gets a pointer to the original. Example:

    • void increment(int n) { (n)++; } — the caller's variable is modified

  • In C there is no built-in reference syntax like C++. "Pass by reference" is achieved by explicitly passing a pointer

Working with int ** (Pointer to Pointer)

  • An int **M represents a pointer to an array of pointers, commonly used for dynamically allocated 2D arrays

  • M[i][j] accesses the element at row i, column j

  • Summing the main diagonal of an n×n matrix: loop i from 0 to n - 1, accumulate M[i][i]

Strings in C

  • A string is a character array terminated by '\0'

  • char str[] = "UCF"; stores 4 characters: 'U', 'C', 'F', '\0'

  • strlen("UCF") returns 3 (does not count the null terminator)

  • "AB CD" has length 5 (the space counts as a character)

  • You cannot compare strings with == (that compares addresses); use strcmp()

  • You cannot copy strings with = (that copies the pointer, not the data); use strcpy()

String Reversal Check

  • To check if one string is the reverse of another: first confirm equal lengths, then compare str1[i] with str2[len - 1 - i] for each index

  • strnlen() is used when you have a maximum capacity and want to avoid reading beyond the buffer

File I/O Steps

The required sequence for any programme that does file I/O:

  1. Declare a FILE * pointer

  1. Open the file with fopen() (specify mode: "r", "w", "a", etc.)

  1. Check whether the file opened successfully (compare against NULL)

  1. Read from or write to the file using fprintf, fscanf, fgets, fputs, etc.

  1. Close the file with fclose()

  1. Set the FILE * pointer to NULL

Skipping the NULL check after fopen() is a common source of segmentation faults in student code.


Formulas / Diagrams

Pointer relationship:

&x → address of x → stored in pointer p

*p → value at that address → gives you x

*(&x) simplifies back to x

String storage for "UCF":

Index

0

1

2

3

Char

U

C

F

\0

strlen returns 3. Array size is 4.


Real-World Applications

Pointers are how operating systems manage memory, how device drivers talk to hardware, and how networking code handles data buffers. The file I/O pattern (open, check, use, close) is universal across systems programming, database engines, and any application that persists data. Understanding that strings are just pointer-accessible byte arrays is also the foundation for text processing, parsers, and compilers.


Common Misconceptions

  • Students often think = can copy one string into another. It cannot. = on character arrays copies an address (for pointers) or is illegal (for arrays). Use strcpy().

  • Students confuse strlen() with the array size. char str[] = "UCF" has strlen of 3 but the array holds 4 bytes (including '\0').

  • Students sometimes believe strcmp("and", "ant") returns a positive value. It returns a negative value, because 'd' (ASCII 100) is less than 't' (ASCII 116).

  • When passing an array to a function, C does not pass a copy of every element. It passes the address of the first element (a pointer). This is a subtle form of pass-by-reference.


Why It Matters / Exam Flags

⚠️ Know that *(&num) simplifies to num. This is a common multiple-choice question.

⚠️ The dereference operator is *. The address-of operator is &. Do not mix them up.

⚠️ strcpy() is in , not or .

⚠️ strcmp(str1, str2) returns a positive value when str1 is alphabetically after str2, zero when equal, and a negative value when str1 comes first.

⚠️ Assignment (=) cannot copy string contents. This is a true/false favourite.

⚠️ When an array is passed to a function, the address of the array is passed, not copies of every element.

⚠️ The NULL pointer does not point to any valid memory address. Expect a fill-in-the-blank on this.


Quick Self-Test

  1. True or false: When an array is passed to a function, C passes a copy of each element.

  1. Fill in the blank: Strings are commonly terminated with a ___.

  1. What does strlen("AB CD") return?

  1. True or false: strcmp("and", "ant") returns a positive value.

  1. Which header file defines strcpy()?

Answers: 1. False 2. null character (\0) 3. 5 4. False 5. string.h


Practice Q&A

Q: Explain the difference between call by value and call by reference.

A: Call by value passes a copy of the argument, so changes inside the function do not affect the original. Call by reference passes the address (a pointer), so the function can modify the original variable through that pointer.

Q: What are the main steps for file I/O in a C programme?

A: Declare a FILE * pointer, open with fopen(), check the pointer is not NULL, read from or write to the file, close with fclose(), then set the pointer to NULL.

Q: What is *(&num) equivalent to?

A: It is equivalent to num. The & takes the address and * immediately dereferences it, giving back the original value.

Q: Why can you not use = to copy one string to another in C?

A: In C, = does not copy the characters of a string. For pointer variables it copies the address (both pointers then point to the same data). For array variables, assignment is not permitted at all. Use strcpy() instead.

Q: char str[] = "UCF"; — how many characters are stored in the array, and what does strlen(str) return?

A: The array stores 4 characters ('U', 'C', 'F', '\0'). strlen(str) returns 3 because it counts characters up to, but not including, the null terminator.


Connections to Other Topics

Pointers are the prerequisite for structs with dynamic members (next set of notes), linked lists, trees, and every data structure that uses heap memory. The pass-by-reference concept reappears whenever a function needs to modify a caller's variable, including setting the size of a dynamically allocated array via a pointer parameter.


Related Terms / Search Tags

pointers C, dereference, address-of operator, pass by value, pass by reference, call by value vs call by reference, int double pointer, string null terminator, strlen vs sizeof, strcmp, strcpy, string.h, FILE pointer, fopen fclose, file I/O C, C 101 UCF, Computer Science I