UPI Study › Courses › Programming in C › Free lessons › Lesson 9.3 Free with a free account
Workshop: Functions with Pointer Parameters
Programming in C · Chapter 9: String Manipulation via Pointers, File Streams, and File HandlingSection 9.3 · lesson 56 of 787 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Programming in C.
Lesson complete
Earn the 3 college credits for Programming in C
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: The Stream I/O Model in C →← Workshop: Navigating Data with Pointer OffsetsNext lesson: The Stream I/O Model in C →
In this lesson
Returning several results and success flags through pointer parameters in C.
- Use pointer parameters to send more than one result back to the caller
- Explain why passing a variable instead of its address fails to compile
- Return a 1 or 0 status while sending data out through a pointer
- Write through an output pointer only on the success path
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (898 words). Read along, or search it.
Opening Question
One Call, More Than One Answer
A function can hand back only one value with return. But Priya's arcade tool needs the lowest and the highest score from one call. And it needs to know whether a typed score was even a number. So how does one call send back more than one thing? Give it the addresses of empty boxes, and let it fill them.
Output Parameters
Filling the Caller's Lo and Hi
Here's the idea from six point five. A function gets a copy of each argument. But when the argument is an address, the copy still points at the caller's box. So main makes two empty boxes, lo and hi, and passes ampersand lo and ampersand hi. Inside, min and max hold those addresses. The function writes through them, with star min and star max, starting from the first score. The row comes in as const int star, plus its length, because it's only read. The loop starts at a plus one, since box zero is already used. min_max itself returns nothing: void. Its results are the two boxes it filled. Build and run: lowest 48, highest 95.
Common Mistake
Forgetting the Ampersand
A common slip is passing lo instead of ampersand lo. So delete the ampersand. Now build again. Clang stops you: incompatible integer to pointer conversion. The function expects an address, and it got a whole number. It even suggests the fix: take the address with ampersand. Passing the address is what gives the function permission to write.
Status and Data
Rescaling Scores Without Dividing by Zero
The arcade also wants every score on a zero to one hundred scale. The lowest becomes zero, and the highest becomes one hundred. normalize gets lo and hi by calling min_max, with the same ampersands. Then it walks the row, and writes each new value over the old one. The rule is score minus lo, times one hundred, divided by hi minus lo, in whole numbers. For 72, that's 2400 over 47, which is 51. One case breaks the rule. If every score is equal, hi minus lo is zero. Dividing by zero is undefined, and it can crash. So normalize checks first. It returns zero, and leaves the row alone. Otherwise, after the loop, it returns one. Build and run. The first call says done equals one, and the row is rescaled. Then three equal scores: done equals zero, and fifty, fifty, fifty is untouched. Two channels: the array carries the data, and the one or zero says whether the job was done.
Quick Check
A Missing Star Loses the Answer
Quick check. Maya has four lap times, and this function should report her fastest lap through a pointer. What does it print? Pause and predict. It prints best lap 31. Look at the line inside the if: best equals p, with no star. That re-aims the function's own copy of the pointer. After the first write, nothing goes through best again, so b in main keeps thirty-one. Twenty-six needs star best equals star p. Fix it, run again, and now it's best lap 26. One missing star, and the answer never reaches the caller.
Success Flag
Parsing a Score Through a Pointer
One more problem. When a player types a score, the arcade gets text, such as 250, or 12x. Turning text into a number can fail. A return value can't be both a score and an error, because any int, even zero or minus one, could be a real score. So parse_score returns yes or no, as one or zero. The number goes out through a pointer parameter, out. The loop walks the text with a char pointer, as in nine point one. C promises the digits are in order, so star p minus the character zero is the digit. Anything that isn't a digit is a failure. So is empty text, and so is a value over a thousand. And star out is written only on the success path, at the very end. main starts v at minus one, which means nothing was written. It tries five texts, one at a time. Build and run. 250 and 1000 pass. 12x fails on the x, the empty text has no digits, and 1001 is over the limit. Every time it fails, v is still minus one, because the function never wrote to it. That's the same shape as scanf, which returns how many items it read, and fills the boxes you gave it.
Story Resolved
Is Zero a Score or a Failure
Sam tested Priya's first parser by typing a blank line. It returned zero for the score, and printed score accepted: zero. "Is zero a real score, or a failure?" Sam asked. "It can't be both," Priya said. She changed the function to return one or zero, and send the number through a pointer. Now a blank line prints try again, and zero stays a good score.
Quick Check
A Failed Call Leaves R Alone
Quick check. safe_div returns zero instead of dividing by zero. main calls it twice with the same r, then prints both results and r. What will it print? Pause and predict. It prints 1 0 4. Nine over two, in whole numbers, is four. The first call succeeds, and writes that four into r. The second call fails before it writes anything, so r keeps the four. Seven was replaced by the first call. And zero is never written: a failure leaves r alone.
Key Takeaways
Addresses In, Flags Out
To recap. Pass an address, like ampersand lo, to let a function write into your box. Inside, write through the pointer with a star. Return one or zero to say whether it worked. And write the value through the pointer only when it did.
About Programming in C at UPI Study
Programming in C (Computer Science 240) is a self-paced online course of 78 video lessons in 11 chapters, from pseudocode and your first program to data types, decisions, loops, functions, arrays, strings, pointers, files, structures, dynamic memory and linked lists. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: C vs C++
Why take it here
Why students take Programming in C with UPI Study
Where the credit goes
Universities that work with NCCRS
Programming in C carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Workshop: Functions with Pointer Parameters
What does incompatible integer to pointer conversion mean in C?
Why does assigning to a pointer parameter not change the caller's variable in C?
How should a C function report an error when any int could be a valid result?
How do you avoid division by zero when rescaling scores to 0 to 100 in C?
About the Programming in C course
How many college credits is Programming in C?
How much does Programming in C cost and what is included?
Which universities accept UPI Study credits?
How long does Programming in C take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Programming in C.
78 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
3 credits · NCCRSEnroll →