Passing Structures to Functions
Passing Structures to Functions
When you combine functions and structures in C, decide whether the function should inspect a record, change the caller's record, or produce an updated record. The parameter declaration controls how the function reaches that data. This lesson assumes you can declare a structure and access its members; the focus is what happens across a function call.
Passing a structure value
C passes arguments by value: each parameter receives the value of its corresponding argument. For a structure parameter, the function receives a separate structure object whose members initially have the argument's values.
Example: A packet counter can be passed to a function that changes only its local structure parameter.
struct Counter {
int packets;
};
void preview(struct Counter c)
{
c.packets += 4;
}If a caller initializes struct Counter q = {6}; and calls preview(q);, the parameter begins with 6 packets and finishes with 10 packets. The caller's q.packets remains 6. The function changed a different structure object, even though both objects initially contained the same member values.
This describes the program's behaviour. A compiler may optimize how the transfer occurs while preserving that behaviour.
Passing an address
A pointer parameter receives an address that the function can use to access another object. C still copies the argument value; here, that value is a pointer rather than a complete structure.
void add_packets(struct Counter *c, int n)
{
c->packets += n;
}Call this function with add_packets(&q, 4);. The address operator & supplies the address of q. Inside the function, c->packets accesses the member of the structure addressed by c; it means the same thing as (*c).packets. Consequently, q.packets increases from 6 to 10.
Reassigning c itself changes only the local pointer parameter. It does not change a pointer variable belonging to the caller. Distinguish changing the pointer from changing the object it addresses.
Returning a structure and choosing an interface
A function can also return a structure value. The caller decides whether to retain that result.
struct Counter added(struct Counter c, int n)
{
c.packets += n;
return c;
}The call added(q, 4); discards the returned value. The assignment q = added(q, 4); replaces q with the returned structure. Returning the structure value is valid even though the local parameter stops existing when the function finishes; returning its address would leave an invalid pointer.
Intended behaviour | Parameter form | Typical call |
|---|---|---|
Work on an independent structure value | struct Counter c | preview(q); |
Update the caller's structure | struct Counter *c | add_packets(&q, 4); |
Inspect through an address | const struct Counter *c | read_packets(&q); |
Produce an updated structure value | struct Counter c, with a structure return type | q = added(q, 4); |
In the third form, const prevents assignments to structure members through that pointer. It does not freeze the object against changes through other permitted access paths.
A structure copy also copies any pointer members as pointer values. It does not automatically duplicate the objects those members address. Two separate structures can therefore refer to shared data.
What to remember
- C copies every argument value, including pointer values.
- A structure parameter is a separate structure object.
- A pointer parameter can provide access to the caller's object.
- Use the returned value explicitly when an update is delivered as a structure result.
- Copied pointer members can still address shared data.
Example: Updating a packet-queue record
A network program tracks pending and sent packets using the following C functions. A returned value is the result a function hands back to its caller. Here, one function returns an updated structure, while the other updates a structure through its address.
struct Queue {
int pending;
int sent;
};
struct Queue preview_send(struct Queue q, int n)
{
q.pending -= n;
q.sent += n;
return q;
}
void commit_send(struct Queue *q, int n)
{
q->pending -= n;
q->sent += n;
}Inside main, the caller initializes struct Queue current = {24, 7};. Assume all packet counts remain within the range of int. Each part continues from the caller's state left by the previous part.
Part a: Discard the returned structure
The caller executes preview_send(current, 5);. Find the returned member values and the member values remaining in current.
The function receives a separate structure parameter containing ________ pending and ________ sent. It subtracts 5 packets from the local pending count and adds 5 packets to the local sent count.
The returned pending count is calculated as 24 − 5 = 19 packets. The returned sent count is calculated as 7 + 5 = 12 packets. These are the members of the local structure returned by the function.
The call is an expression statement with no receiving assignment, so that returned structure is discarded. No pointer to current was passed, and the function does not assign to current.
Answer: The function returns 19 pending packets and 12 sent packets. The caller's current still contains 24 pending packets and 7 sent packets.
Part b: Retain the returned structure
The caller next executes struct Queue next = preview_send(current, 8);. Find the members of both next and current.
The previous call did not change current. The new local structure parameter therefore starts with ________ pending and ________ sent.
Subtract the requested 8 packets from the pending count: 24 − 8 = 16 pending packets. Add the same 8 packets to the sent count: 7 + 8 = 15 sent packets.
The function returns this updated structure value, and the declaration initializes next with it. The source object current remains unchanged because the function worked on its own structure parameter.
Answer: next contains 16 pending packets and 15 sent packets. current still contains 24 pending packets and 7 sent packets.
Part c: Update through an address
The caller finally executes commit_send(¤t, 6);. Find the members of current and determine whether next changes.
The argument is the address of current. The function receives a copy of that address, so each arrow expression accesses a member of the caller's current object.
The pending count becomes 24 − 6 = 18 pending packets. The sent count becomes 7 + 6 = 13 sent packets. Their sum is 18 + 13 = 31 packets, matching the starting total of 24 + 7 = 31 packets.
The separate object next is not addressed by this call. Its members therefore retain the values calculated in part b.
Answer: current contains 18 pending packets and 13 sent packets. next remains at 16 pending packets and 15 sent packets.
A monitoring program uses the following C declarations. Complete the argument expressions for the two function calls using the local variable named meter. Write each argument expression with no spaces or parentheses.
struct Meter {
int pulses;
};
void display(struct Meter reading);
void clear(struct Meter *reading);
void service(void)
{
struct Meter meter = {48};
/* Calls to display and clear go here. */
}To call display, the argument expression is . To call clear with access to the same local structure, the argument expression is .
A pulse-counting program executes the following C code. What is the final value of r.pulses immediately before main returns? Answer in pulses as an exact integer, to zero decimal places.
struct Meter {
int pulses;
};
struct Meter advance(struct Meter m, int n)
{
m.pulses += n;
return m;
}
int main(void)
{
struct Meter r = {42};
advance(r, 9);
r = advance(r, 4);
return 0;
}