Mutable Struct APIs
Lesson 25 of 48 in Coddy's C for Dart Developers course.
A Dart method has an implicit receiver, this, and a name starting with an underscore is private to its library rather than to its class. C has no methods: state-changing operations are free functions that take an explicit struct pointer, and a header documents the API but does not create classes, methods or private fields.
State-changing functions accept explicit struct pointers. Validate first so rejected operations preserve state. C has no implicit receiver; a->balance is shorthand for (*a).balance.
Dart example
class Account {
int balance;
Account(this.balance);
bool spend(int amount) {
if (amount < 0 || amount > balance) return false;
balance -= amount;
return true;
}
}
void main() {
final account = Account(5);
account.spend(2);
account.spend(9);
print(account.balance);
}
Dart output:
3C example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
struct Account {int balance;};
static bool account_spend(struct Account *a,int amount){
if(amount<0||amount>a->balance)return false;
a->balance-=amount;return true;
}
int main(void) {
struct Account account={5};account_spend(&account,2);account_spend(&account,9);printf("%d\n",account.balance);
return 0;
}
C output:
3Challenge
EasyAccount contains integer balance. Implement withdraw(account,amount): return false without mutation for negative or unaffordable amounts; otherwise subtract and return true. Zero is valid. Input is balance and amount; output success:balance. Driver also probes independent accounts and rejection atomicity.
Edit the unlocked source and header files. Keep the declared public API compatible with the visible locked main.c, which calls it directly. Do not add another main. All numeric task fixtures are small enough to avoid overflow unless the task explicitly checks a range. Print only the specified output; the driver supplies input parsing and output when shown.
Try it yourself
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
#include "lesson.h"
int main(void){Account a;int amount;if(scanf("%d%d",&a.balance,&amount)!=2)return 1;Account x={4},y={9};if(withdraw(&x,5)||x.balance!=4||withdraw(&x,-1)||x.balance!=4||!withdraw(&x,2)||x.balance!=2||y.balance!=9)return 2;int ok=withdraw(&a,amount);printf("%d:%d\n",ok,a.balance);return 0;}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in C for Dart Developers
1Compile and Compute
Run C Across FilesPrinting and CommentsTypes and InitializationInteger Division and RangeFloating ArithmeticRecap: Receipt Total4Functions and Interfaces
Function Arguments Are ValuesHeaders and LinkageMultiple Results via PointersConst Views and MutationRecap: Price Breakdown2Decisions and Loops
Booleans and BranchesLogical OperatorsSwitch and Enumerated CasesLoop Bounds and ProgressRecap: Admission Rules5Pointers and Structs
Addresses and DereferencingPointer Steps and BoundariesRecords as Struct ValuesMutable Struct APIsRecap: Point TranslationPractice on your own: Online C compiler