Deep C Dives: Value Structs
Written by Mike James   
Wednesday, 27 August 2025
Article Index
Deep C Dives: Value Structs
Value Semantics
Padding
Final Thoughts

The C struct is a value type and this is often forgotten or ignored? Find out why it has an important role to play in this extract from my book, Deep C Dives.

Deep C Dives
Adventures in C

By Mike James

Cdive360

Buy from Amazon.

Contents

Preface
Prolog C
Dive

  1. All You Need Are Bits
  2. These aren’t the types you’re looking for
  3. Type Casting
  4. Expressions
  5. Bits and More Bits
        Extract:
    Bits!
  6. The Brilliant But Evil for 
  7. Into the Void 
  8. Blocks, Stacks and Locals
  9. Static Storage
  10. Pointers
  11. The Array and Pointer Arithmetic
  12. Heap, The Third Memory Allocation
  13. First Class Functions
        Extract:
    First Class Functions
  14. Structs and Objects
        ExtractValue Structs ***NEW!
  15. The Union
  16. Undefined Behavior
  17. Exceptions and the Long Jump

<ASIN:B0D6LZZQ8R>

Dive 14 

Structs and Objects

Object-oriented programming is an exceptionally bad idea which could only have originated in California.”

Edsger Dijkstra

 

Apart from the array, the struct is the only other sophisticated data structure that C provides. As things turn out, the array and the struct are enough for most things. At the most basic level, the struct is the C implementation of the data record, but the data record is also where object-oriented programming starts. This might seem surprising, but it is true that in most languages there is little difference between an object and a record. To understand C’s approach, the all purpose struct, let’s start with the basics.

The Struct

You were probably introduced to the struct, short for “structure”, as a sort of array that allows different types as elements. For example:

struct{
	int myfield1;
float myfield2;
} myStruct;

This creates a struct with two fields – one an int and the other a float. These fields are grouped into the structure in the same way as elements are in an array – except that they are of different types. Of course, you access the fields using the familiar dot notation:

myStruct.myfield1 = 42;
myStruct.myfield2 = 4.2;

This declaration is of the standard form:

type variable;

where type is the complete struct definition and the variable is the name at the end of the type.

For example:

struct{
int myfield1;
float myfield2;
}

and the variable is myStruct. This form of struct type is called an “anonymous struct” because the type itself lacks a name other than struct.

You can take this a little further by giving the type a name, a tag. This allows you to reuse it. For example:

struct myStructType{
int myfield1;
float myfield2;
}; struct myStructType myStruct;

You can use struct myStructType as often as you like to create as many structs of the type as you need.

The full name of the type is struct myStructType and you can get rid of the struct part of the name by using a typdef

typedef struct {
int myfield1;
float myfield2;
}myStructType;
myStructType myStruct;

Now the anonymous struct has been given the name myStructType and it can be used in the standard way to declare as many variables as you like. Introduced like this, the C struct declaration doesn’t seem in the least bit illogical and it is quite obvious why there are different ways of declaring a struct.

The main use of the anonymous struct is to declare struct fields in other structs. For example:

struct{
struct {int a,b;} myStruct1;
float value;
}myStruct2;
myStruct2.value=4.2;
myStruct2.myStruct1.a=42;

There are many other struct idioms that make using structs more compact. 



Last Updated ( Wednesday, 27 August 2025 )