YOUR C LEARNING COMPANION

Find your next idea.

Learning path
Designing dataChapter 31 of 73

Structure values and functions

Understand record copying, pointer members, value parameters, returned results, semantic equality, and serialization limits.

5 min readCore conceptsC17 baseline
Offline reading
In this chapter8 sections
0% through this chapter

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

C17 · illustrative fragment
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

C17 · illustrative fragment
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.

Copying a view
first.value
points to score
second.value
points to the same score
Two pointer member objects, one target. A structure copy does not extend the target’s lifetime.

3. Structure parameters are passed by value

Complete C17 program
#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:

C17 · illustrative fragment
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

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.
Explain it in your own words

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.

A function receives a structure by value. Which change can affect an object shared with its caller?
Read the explanation for every choice
  1. Assigning a new integer directly to the copied structure’s int member.

    That member belongs to the separate parameter object.

  2. 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.

  3. 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.

CHAPTER 31 / 73

In this chapter

Structure values and functions

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping