ACtE0302 Pointers, structure and data files in C programming¶
Pointer basics and arithmetic¶
A pointer stores an address of an object or function of a compatible type.
| Expression | Meaning |
|---|---|
p |
pointer value (address) |
&x |
address of x |
*p |
object pointed to by p |
Pointer arithmetic is scaled by the pointed-to type size.
If int *p points to a[0], then p+1 points to a[1], not one byte later unless sizeof(int) == 1.
Valid arithmetic patterns:
- pointer plus or minus integer within the same array object;
- difference of two pointers into the same array gives element count difference.
Invalid or dangerous:
- arithmetic on
void *is not standard C arithmetic; - dereferencing an invalid, null, or out-of-bounds pointer is undefined behavior.
Pointer and array relationship¶
Closely related but not identical facts:
- array name in most expressions decays to pointer to first element;
sizeof arraygives total array size in bytes when the actual array object is in scope;sizeof pointergives pointer size, not array size;- arrays are not assignable objects.
Example:
Here a decays to &a[0] in the initialization.
Indexing and dereferencing are equivalent within the array:
Therefore the third element is arr[2], equivalently *(arr + 2).
Passing pointer to function¶
Pass a pointer when the function must:
- modify caller-visible data;
- avoid copying large objects;
- work with arrays or dynamic memory;
- simulate pass-by-reference in C.
Example swap:
Trap: passing the value itself would not let the callee change the caller's original object.
Structures and unions¶
| Feature | struct |
union |
|---|---|---|
| Storage | separate storage for each member | all members share the same storage |
| Size | at least sum of members plus padding | at least size of largest member plus alignment |
| Simultaneous valid members | yes | only one active stored representation at a time in normal use |
| Use case | records with multiple fields | variant data / memory-saving overlays |
Structure operations:
- member access with
.on object; - member access with
->through pointer; - structures of same type can be assigned as whole objects in C.
Arrays of structures and structure to function¶
Example pattern:
Passing choices:
- pass by value copies the whole structure;
- pass pointer for efficiency or mutability;
- arrays of structures are contiguous structure objects.
File I/O basics in C¶
The library type FILE * represents a stream.
Open and close:
| Function | Purpose |
|---|---|
fopen |
open a file and return stream |
fclose |
close stream |
fflush |
flush output buffer for output/update streams |
Common modes:
| Mode | Meaning |
|---|---|
"r" |
read existing text file |
"w" |
write text file, truncate/create |
"a" |
append text file |
"rb", "wb", "ab" |
binary variants |
"r+", "w+", "a+" |
update modes |
Sequential and random access¶
| Access style | Typical functions | Recognition |
|---|---|---|
| Sequential | fgetc, fgets, fprintf, fscanf, fread, fwrite |
process in current stream order |
| Random | fseek, ftell, rewind |
reposition file pointer |
Binary block I/O:
fwrite and fread are conventionally used for raw binary block I/O, but the ISO C functions operate on streams and are not restricted to files opened in binary mode.
fseek(fp, offset, origin) uses SEEK_SET, SEEK_CUR, or SEEK_END as origin.
File-error recognition¶
| Function / indicator | Role |
|---|---|
NULL from fopen |
open failed |
feof |
end-of-file indicator state |
ferror |
stream error indicator |
perror |
prints message for current errno |
Trap: while (!feof(fp)) is a classic bug pattern because EOF is only known after a read attempt fails.
Pointer-structure-file revision box¶
- Pointer arithmetic moves in units of the pointed type.
- Array name often decays to pointer to first element, but array and pointer are not identical objects.
structstores all members;unionoverlays them..is for structure object,->is for structure pointer.fopenreturnsFILE *; check forNULL.- Random file access uses
fseek,ftell, andrewind.