Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • ๐Ÿ›ฃ๏ธC Plus Plus: Docs
  • Build
    • ๐Ÿ”ฅBuild Process
    • ๐Ÿ”งConnect multiple C++ Files
  • Code
    • โฎ๏ธPre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • ๐Ÿช…Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • ๐ŸŽญCasting
    • ๐Ÿ”ƒOperator overloading
      • Available Operators
      • Examples of operator overloading
    • ๐Ÿ—บ๏ธNamespace
      • Namespace Example
      • Using directive
    • ๐Ÿ•ต๏ธHeader File
      • For C++ Classes
    • ๐Ÿ—๏ธStructure
      • Struct vs Class
      • Public vs Private inheritance
    • ๐ŸขClasses
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • ๐Ÿง Memory Introduction
    • โœจHeap and Stack
    • ๐ŸŽฏPointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • ๐Ÿ‘จโ€๐ŸญHelper function
    • ๐ŸกVector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • โ™ป๏ธUnion
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • ๐ŸงตThreading
      • std::thread
      • Detach a thread
  • Misc
    • ๐Ÿ—‚๏ธExecution Order
    • ๐Ÿง Print memory
Powered by GitBook
On this page
  1. Memory
  2. Union

Type Punning, part 3

In last session, we read struct as a array, now we will extend on this with two struct implementation.

Here is what it would look like:

#include <iostream>

struct Vector1
{
    int a, b;
};

struct Vector2
{
    int a, b, c, d;

    Vector1* getVector1() {
        // Return address of first element as Vector1
        return (Vector1*) &a;
    }
};

void printVector1(Vector1& vec1) {
    std::cout << vec1.a << ", " << vec1.b << std::endl;
}

int main(int argc, char const *argv[])
{
    Vector2 v2 = {1,2,3,4};
    
    Vector1* v1 = v2.getVector1();

    printVector1(v1[0]);
    printVector1(v1[1]);

    return 0;
}

Try it here.

// Output
1, 2
3, 4

Let's see how it works:

  1. We have 2 structs, Vector1 and Vector2.

  2. Vector1 contains 2 integers and Vector2 constains 4 integers.

  3. It can be said, Vector2 can be read as two Vector1 objects.

  4. In the example above, we are doing the same thing. We create a object of Vector2 that store 4 integer variables and then read them as two Vector1 objects (as an array).

  5. Thus, we successfully read a Vector2 data as two Vector1 data.

PreviousType Punning, part 2NextUnion, part 1

Last updated 10 months ago

โ™ป๏ธ