I have much more experience with PAWN scripting language, but I'm digging into K&R book (I think it is it), and trying some stuff on my own.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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(&itemList[i], &singleItem, sizeof(singleItem));
}
}
void dumpItems() {
Item
*currentItem;
size_t
i = 0;
char
str[256];
for(; i != ITEMS_COUNT; ++i) {
currentItem = &itemList[i];
//Not safe sprintf!
sprintf(str, "Row %d: { dbID: %d,", i + 1, currentItem->dbID);
if(TYPE_PERSON == currentItem->type) {
sprintf(str, "%s name: %s, surname: %s }", str, currentItem->chosenItem.PersonItem.name, currentItem->chosenItem.PersonItem.surname);
} else {
sprintf(str, "%s company name: %s }", str, currentItem->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?
inline
is C++'s alternative/answer/attempt at solving macro issues – Elias Van Ootegem 2 days ago