Pointers with Strings and Structures
char pointers versus char arrays, walking a string by pointer, pointers to structures, and the arrow operator.
Pointers Meet the Big Data Types
Two chapters of data types, strings and structs, each left one pointer-shaped gap. This lesson fills both: the real difference between a char array and a char pointer, and the arrow operator that makes struct pointers pleasant, closing chapter 10's copy-cost problem in the process.
char Arrays Versus char Pointers
Two declarations that print identically and differ profoundly:
buffer is an array: six bytes of your own, initialized by copying the literal, writable, buffer[0] = 'H' is fine. message is a pointer aimed at the literal itself, wherever the compiler stored it, and here is the rule with teeth: modifying a string literal is undefined behaviour. message[0] = 'H' compiles in this dialect and dies at run time on most modern systems, which store literals in read-only memory. The working discipline: array when you need to own and edit text, pointer-to-literal only for text you will never touch, and walking a string by pointer is the same p < end or *p != '\0' walk as ever, because a string is an array being itself.
Struct Pointers and the Arrow
Aim a pointer at a struct and the dot needs help: *p.member parses as *(p.member), precedence putting the dot first, wrong. The parenthesized form works, and its shorthand is one of C's most typed operators:
p->member means (*p).member: follow the pointer, select the member. Reading and writing through the arrow reaches the original struct, as the last line proves, star changed through p. The rule of thumb exams test: dot for a struct variable, arrow for a struct pointer, and when arrows chain with dots (list->head.value), each operator matches what is on its left.
The Chapter 10 Fix: Pass the Address
Large structs copying whole per call was chapter 10's open wound. The cure is now three characters:
applyBonus receives eight bytes of address instead of a record copy, whatever the record's size, and its writes land in the caller's struct: the modify-the-original power and the no-copy economy in one move. This is how real C passes records, and the earlier by-value guidance completes into the full professional rule: small read-only structs may travel by value; everything else travels by address, with const marking read-only pointer parameters in codebases that use it.
The same shape powers struct out-parameters (a function filling a record the caller owns) and, one chapter ahead, the nodes of linked lists, where structs contain pointers to their own type and the arrow operator becomes the primary way anything gets done.
Arrays of Structs, Pointer Style
The two big types compose: students + i points at record i, and (students + i)->marks is students[i].marks in pointer clothes. Real code overwhelmingly writes the index form for clarity, but the equivalence is exam material, and functions receiving struct Student *table, int count are last lesson's pointer-plus-count convention carrying records instead of ints, the shape the capstone's record manager will use throughout.
Key Takeaways
char buffer[] = "text"copies the literal into writable storage you own;char *p = "text"aims at the literal, and modifying a literal is undefined behaviour.p->memberis(*p).member: dot for struct variables, arrow for struct pointers, each operator matching its left-hand side.- Writes through a struct pointer reach the original: passing
&recordgives functions modify access at address cost, the fix for chapter 10's copy drawback. - The full rule: small read-only structs by value, everything else by address.
(table + i)->memberequalstable[i].member; struct-pointer-plus-count is the record-passing convention ahead.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Pointers with Strings and Structures - Quiz
Test your understanding of the lesson.
Practice Exercises
Bonus Through the Arrow
Write void applyBonus(struct Player *p, int extra) that adds to the record's goals through the arrow operator, and a main that reads a player (number and goals) plus a bonus, applies it by address, and prints the updated record.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!