Page 1 of 1

I am A needy little idiot

Posted: April 5th, 2017, 2:50 am
by Alacaster
If I wanted to define a variable type that was an array of multiple character arrays like for an inventory or a list of words or sentences.
Could you use a struct?
like

list Inventory[4]

and it would have four 20 byte strings 0, 1, 2, and 3

Really, I want to make my own custom variable that I can put in a header file.
If this is too complicated of a question just ignore me and my ignorant little unworthy self.
Also do you have any good suggestions for C++ Programming books.

Re: I am A needy little idiot

Posted: April 5th, 2017, 4:50 am
by albinopapa

Code: Select all

struct Inventory
{
     char item_name[20]
};

Inventory items[4]
Something like this would let the compiler know the size of the Inventory struct at compile time, which is perfect for serializing ( saving to disk ) since all the memory is contiguous.

As long as your arrays inside the struct are defined as static arrays like it is here, you can store the struct with a single write command.

Code: Select all

#include <fstream>
std::ofstream file("inventorylist.inv", std::ios::binary);
file.write(reinterpret_cast<char*>( items ), sizeof(Inventory) * 4 );
file.close();
However, if your struct just has heap allocated pointers or something like std::string, then you will have to have a write command for each element in the struct in the order that you want them in because the data is not in the same memory address range as the Inventory object itself.



As for the book thing, don't have any suggestions. There are a crap ton of resources online that you can read through and learn the ins and outs of C++.

cplusplus.com and
cppreference.com

are just two of the ones I use. I know cplusplus.com has a tutorials series that you can go through, plus lots of examples on how to use everything in the Standard Template Library, plus a forum where people have asked questions and have had them answered. It's like an all in one package.

Re: I am A needy little idiot

Posted: April 5th, 2017, 11:17 pm
by Alacaster
Thanks

Re: I am A needy little idiot

Posted: April 6th, 2017, 12:11 am
by albinopapa
Was wondering if that is what you wanted by your description, glad it helped.

Re: I am A needy little idiot

Posted: April 6th, 2017, 1:20 am
by chili
As for books, I have never read them, but a good intro seems to be C++ Primer, and a good in-depth look would be Bjarne's book.

Re: I am A needy little idiot

Posted: April 6th, 2017, 9:12 pm
by Byteraver
Hi

Concerning books, I really liked the C++ books from Herbert Schildt, he explains in a very clear style in my opinion (just c++ though, no windows / graphics programming and only C98c98):
https://images-na.ssl-images-amazon.com ... SV6NQL.jpg

The book(s) by Peter Walsh are not bad either (DirectX 10, maybe there's a newer version now):
https://images-na.ssl-images-amazon.com ... 3,200_.jpg

Re: I am A needy little idiot

Posted: April 7th, 2017, 9:16 am
by simonjoel
chili wrote:As for books, I have never read them, but a good intro seems to be C++ Primer, and a good in-depth look would be Bjarne's book.
do you have any recommendation for Directx game develop books, I mean, some book to read after the C++ primer and more about game develop.