OSC.010: C Structures — Records, Member Access, Value Copies and Pointer Parameters

Colored-pencil technical illustration of related memory fields and two separate records, with a pointer connecting to one record.

Open-Source C Programming • Foundational • Estimated study time: 45 minutes.

A C structure groups related values under a single type. After learning strings and buffer capacity, learners can combine a rack identifier, a temperature and a validity flag into one record without managing disconnected variables.

Prerequisites and objectives

Use a C17 compiler. Review arrays and pointer parameters. By completion, learners can define and initialize a structure, access members directly and through pointers, distinguish value copying from mutation, maintain arrays of records and explain why padding matters.

Structure definitions and member access

A structure definition describes named members and their types; declaring a variable of that type creates an object. C99 designated initializers identify members by name, and unspecified members in an aggregate initializer are initialized to zero or the appropriate null value. The GNU C Reference Manual explains structure declarations and pointer access. Use the dot operator for an object and the arrow operator for a valid pointer to an object. The expression p->temp_c is equivalent to (*p).temp_c.

Neso Academy introduces structures in C. Watch at source.

Value copies and pointer parameters

Assignment between objects of the same structure type copies member values, including embedded arrays. A pointer member, by contrast, copies an address rather than cloning its target. C passes arguments by value: a function accepting a structure receives a separate value, while a function accepting a pointer can modify the pointed-to object. A null check protects against null dereference but cannot prove that a non-null pointer is valid. A const-qualified pointed-to type prevents modification through that access path.

Bro Code’s C Programming Full Course (2025) explains structs at 05:35:54, arrays of structs at 05:48:08 and pointers at 05:54:48. Watch at source.

Laboratory: synthetic rack readings

The laboratory uses fixed-size identifiers and integer temperatures. The accepted interval of −40 to 125 °C is an illustrative validation rule, not a hardware specification. A separate validity flag distinguishes missing readings from a measured zero. Save the following complete program as reading.c:

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>

struct Reading {
    char id[8];
    int temp_c;
    bool valid;
};

static void change_copy(struct Reading r) {
    r.temp_c = 99;
    assert(r.temp_c == 99);
}

static bool set_temp(struct Reading *r, int value) {
    if (r == NULL || value < -40 || value > 125) return false;
    r->temp_c = value;
    r->valid = true;
    return true;
}

int main(void) {
    struct Reading a = {.id = "R1", .temp_c = 24, .valid = true};
    struct Reading b = a;
    b.id[0] = 'X';
    assert(a.id[0] == 'R' && b.id[0] == 'X');
    puts("PASS array member copied");
    change_copy(a);
    assert(a.temp_c == 24);
    puts("PASS by-value isolation");
    assert(set_temp(&a, 30) && a.temp_c == 30);
    puts("PASS pointer update");
    assert(!set_temp(NULL, 30) && !set_temp(&a, 200));
    assert(a.temp_c == 30);
    puts("PASS rejected input");
    struct Reading rack[] = {a, b, {.id = "R3"}};
    assert(sizeof rack / sizeof rack[0] == 3 && !rack[2].valid);
    puts("PASS array and missing reading");
    printf("sizeof Reading=%zu; temp_c offset=%zu\n",
           sizeof a, offsetof(struct Reading, temp_c));
    return 0;
}

Compile and run the program with strict C17 warnings using the following commands:

cc -std=c17 -Wall -Wextra -Wpedantic -Werror reading.c -o reading
./reading

GCC 14.2.0 on x86-64 compiled and executed the program successfully with strict warnings enabled, producing the following output:

PASS array member copied
PASS by-value isolation
PASS pointer update
PASS rejected input
PASS array and missing reading
sizeof Reading=16; temp_c offset=8

Memory layout and safe engineering practice

The observed object occupies 16 bytes and temp_c begins at offset 8 on the tested platform. C permits internal and trailing padding to satisfy alignment; another compiler or architecture can produce different sizes. Use sizeof and offsetof rather than assuming fixed offsets. Never transmit raw structure bytes as a portable network protocol: explicitly encode each field, specify byte order and validate incoming lengths. Keep experiments local and synthetic rather than writing to production telemetry or memory-mapped hardware.

Exercises and answer key

Exercise A: Predict a.id[0] after b.id[0] changes. Exercise B: Explain why change_copy cannot update a. Exercise C: Predict the outcome of set_temp(&a, 0). Exercise D: Explain why the third record is initially invalid. Exercise E: Explain why sizeof cannot define a portable packet format. Exercise F: Declare and initialize a new Reading named sensor with ID R4, temperature 21 and valid true. Given a pointer p to sensor, write an expression that reads its temperature.

Answers: A. R, because the embedded array was copied. B. The parameter receives a separate value. C. The call succeeds, temp_c becomes zero and valid becomes true. D. Its omitted valid member is zero-initialized. E. Padding, alignment, representation and byte order can differ between systems. F. Use the following declaration and member access: struct Reading sensor = {.id = "R4", .temp_c = 21, .valid = true}; then p->temp_c when p points to sensor.

Conclusion

Structures combine related fields, while pointers enable controlled mutation of existing objects. Apply the function parameter principles and test copies, pointer writes, invalid inputs and missing observations separately. Consult the GNU C Language Manual structures chapter and member access reference for additional detail.

Editor’s Note: We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please scan the QR code below

BitcoinVersus.Tech donation QR code for 5,000 satoshis

For mobile scanning, open the article on another display and scan the QR code with a Bitcoin wallet. The voluntary donation amount is 5,000 sats. Verify the receiving address 3C9o19EH5HSiwEPyCTmEKzxhNCbo2X6TTb before sending.

BitcoinVersus.Tech publishes technology and financial journalism for informational purposes and does not provide financial, investment, or legal advice.

Leave a Reply