C code displaying random datasets I have much more experience with PAWN scripting language, but I'm digging into K&amp;R book (I think it is it), and trying some stuff on my own.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

#define ITEMS_COUNT 16

unsigned int randr(unsigned int min, unsigned int max);
inline short randomType();
inline char* randomCompanyName();
inline char* randomPersonName();
inline char* randomPersonSurname();
void populateItems();
void dumpItems();

typedef unsigned short int DBID;

typedef struct {
    char name[64];
    char surname[64];
} Person;

typedef struct {
    char name[128];
} Company;

typedef struct {
    unsigned short int type;
    DBID dbID;
    union {
        Person PersonItem;
        Company CompanyItem;
    } chosenItem;
} Item;

enum E_TYPE {
    TYPE_PERSON,
    TYPE_COMPANY
};

Item
    itemList[ITEMS_COUNT];

int main(int argc, char* argv[]) {
    /* Init stuff */
    srand(time(NULL));

    populateItems();
    dumpItems();
    return 0;
}

void populateItems() {
    Item
        singleItem;
    size_t
        i = 0;

    for(; i != ITEMS_COUNT; ++i) {
        singleItem.dbID =   i + 1;
        singleItem.type = randomType();

        if(TYPE_PERSON == singleItem.type) {
            strncpy(singleItem.chosenItem.PersonItem.name, randomPersonName(), sizeof(singleItem.chosenItem.PersonItem.name));
            strncpy(singleItem.chosenItem.PersonItem.surname, randomPersonSurname(), sizeof(singleItem.chosenItem.PersonItem.surname));
        } else {
            strncpy(singleItem.chosenItem.CompanyItem.name, randomCompanyName(), sizeof(singleItem.chosenItem.CompanyItem.name));
        }
        memcpy(&amp;itemList[i], &amp;singleItem, sizeof(singleItem));
    }
}

void dumpItems() {
    Item
        *currentItem;
    size_t
        i = 0;
    char
        str[256];

    for(; i != ITEMS_COUNT; ++i) {
        currentItem = &amp;itemList[i];

        //Not safe sprintf!
        sprintf(str, "Row %d: { dbID: %d,", i + 1, currentItem-&gt;dbID);

        if(TYPE_PERSON == currentItem-&gt;type) {
            sprintf(str, "%s name: %s, surname: %s }", str, currentItem-&gt;chosenItem.PersonItem.name, currentItem-&gt;chosenItem.PersonItem.surname);
        } else {
            sprintf(str, "%s company name: %s }", str, currentItem-&gt;chosenItem.CompanyItem.name);
        }

        printf("%s\n", str);
    }
}

inline short randomType() {
    return randr(0, 1);
}

inline char* randomCompanyName() {
    static char* 
        nameList[32] = {
        "Microsoft",
        "Valve",
        "Vamonos pest",
        "Hello folks!",
        "Just a program inc"
    };
    return nameList[randr(0, 4)];
}

inline char* randomPersonName() {
    static char* 
        nameList[32] = {
        "John",
        "Mike",
        "Emily",
        "Jessica",
        "Steven"
    };
    return nameList[randr(0, 4)];
}

inline char* randomPersonSurname() {
    static char* 
        nameList[32]  = {
        "Ross",
        "Litt",
        "Hellyeah"
    };
    return nameList[randr(0, 2)];
}

unsigned int randr(unsigned int min, unsigned int max)
{
    double 
        scaled = (double) rand() / RAND_MAX;

    return (max - min + 1) * scaled + min;
}


I'd like you to tell me if I've made any major mistakes, and how's my coding style. Additionally I have a question about unions. I thought that it simply pointed to regions, and that you didn't have to care later which one you are using, like:

//instead of 
singleItem.chosenItem.PersonItem.name
//use
singleItem.chosenItem.name


which would point at either PersonItem.name, or CompanyItem.name. How to achieve somthing similar?
My little bit of input:


inline is a C++ keyword, and it's only a hint to the compiler. The only reason to explicitly use inline is when you define function's body inside a header file, which is included in more than one source file.
Avoid basic data types like int, use types from stdint.h instead
Use comments to explain why are you doing something, and what are function arguments for
No reason to break a line after declaring a type.
No reason to give nameList[32] a size - the compiler will guess it on its own. Just do it as

static char* nameList[]  = {
    "Ross",
    "Litt",
    "Hellyeah"
}; 

Use sizeof(nameList) - 1 instead of a magic number to indicate last array index - there will be less changes if you ever add more names.


Regarding your union questions:

union is kinda like a struct; the only difference is how it's handled in memory.
Since your union has two fields (PersonItem and CompanyItem), you must say which one is to be used. Every object created with your struct will contain both fields. What you're trying to do is called polymorphism, and you have it with classes in C++. Original C does not have such option.