Structure values and functions
Understand record copying, pointer members, value parameters, returned results, semantic equality, and serialization limits.
In this chapter8 sections
What gets copied when you pass a structure to a function? Its member values are copied, but a pointer member can still refer to shared storage. Following that distinction explains which changes stay local and which can affect other code.
1. A record can be copied as a value
struct Record {
int id;
char label[4];
};
struct Record original = {7, "Cat"};
struct Record copy = original;
copy.label[0] = 'B';
The second record is a separate object. Its contained array elements are copied as part of the structure value, even though arrays cannot ordinarily be assigned with a standalone array assignment. Editing the copied label does not edit the original array. This is one reason the enclosing type matters: an operation available for a structure is not automatically available for every member used alone.
Initialization such as struct Record copy = original; supplies a new object’s initial value. A later copy = original; assigns to an existing modifiable structure object of a compatible type. Neither operation promises that padding bytes become a stable canonical representation. Think about member values and identity before byte-for-byte images.
2. Pointer members copy pointer values
struct View { int *value; };
int score = 4;
struct View first = {&score};
struct View second = first;
*second.value = 9;
The two structure objects contain two pointer members, both designating score. Copying the record did not create another integer target. This is often called a shallow copy when a record refers to separately managed data. A deep-copy operation must explicitly allocate or otherwise create new targets, copy the intended contents, and handle failure and cleanup.
If the original pointer was a borrowed view, the copy remains subject to the same target lifetime. If the original pointer represented ownership, copying its bits does not establish two independent rights to free one allocation. Decide whether copies borrow, share through an explicit policy, or own distinct allocations.
- first.value
- points to score
- second.value
- points to the same score
3. Structure parameters are passed by value
#include <assert.h>
struct Point { int x; int y; };
static struct Point with_x(struct Point point, int x)
{
point.x = x;
return point;
}
int main(void)
{
struct Point start = {2, 3};
struct Point moved = with_x(start, 7);
assert(start.x == 2);
assert(moved.x == 7 && moved.y == 3);
return 0;
}
The function receives its own parameter object, changes that copy, and returns a structure value. The caller’s start stays unchanged. Returning the value is valid. Returning a pointer to an ordinary local object would not keep that object alive for the caller to use after the call. A compiler may optimize physical copying while preserving these observable semantics.
A pointer parameter, for example void set_x(struct Point *point, int x), can instead update a caller-provided live object. That interface needs a valid target contract. Passing by pointer is not automatically faster or safer; choose the interface that communicates intended effects and lifetime clearly.
4. Equality is a design decision
C does not provide structure operands for ==. Decide which member values define equality in your problem. Include <stdbool.h> for bool in this C17 fragment. For the two integer coordinates, member-wise comparison is straightforward:
struct Point { int x; int y; };
struct Point first = {2, 3};
struct Point second = {2, 3};
bool same = first.x == second.x && first.y == second.y;
memcmp compares bytes, not the chosen meaning of a record. Padding can differ, and member values may have more than one representation. If a record contains text pointers, decide whether equality means the same target or equal text contents; those are different comparisons. Floating-point fields add their own equality rules and NaN questions.
5. Copying does not define a file format
A structure’s in-memory representation depends on the implementation and types. Writing its raw bytes to a file can expose padding, byte order, pointer representations, and version-specific layout. A durable format should define field meanings and encoding explicitly. Copying a C value within a program and serializing a record for another system are different tasks.
Primary references and further reading
- WG14 N1570 — public C11 committee draft: 6.2.6.1, 6.5.2.2, 6.5.9, 6.5.16.1, 6.8.6.4. Stable core rules used for the C17 baseline; this is not the final ISO publication.
Before you move on
- Copying a structure copies its member values.
- A copied pointer member can keep two records connected to the same target.
- Logical comparison and external serialization require policies beyond copying object bytes.
Keep these ideas separate
- Structure copy / Deep copy
- A value copy preserves pointer values; a deep-copy policy deliberately creates and manages independent referenced storage.
Which changes affect only a copied record, and which can affect a shared pointer target?
Optional: say it aloud or keep a note. You can continue whenever you are ready.Check your understanding
Optional. Answers are not saved or sent, and reading stays unrestricted.
Read the explanation for every choice
- Assigning a new integer directly to the copied structure’s int member.
That member belongs to the separate parameter object.
- A permitted write through a copied pointer member that still refers to the shared object.
✓ Correct reasoning. Copying a structure does not automatically copy the objects its pointer members refer to.
- Changing any parameter necessarily replaces the caller’s whole structure.
The structure parameter is a separate object.
C17
Ready for the next idea?
Mark this chapter as read whenever you feel ready.