Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post Posted: April 5th, 2017, 2:50 am 
 

Joined: October 16th, 2016, 5:08 pm
Posts: 12
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.

_________________
You can't be stabbed in the back if you don't have any friends.
Why live? Cause why not.


Last edited by Alacaster on April 8th, 2017, 12:01 am, edited 1 time in total.

Top 
 Profile  
Reply with quote  
 Post Posted: April 5th, 2017, 4:50 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2546
Location: Oklahoma, United States
Code:
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:
#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.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post Posted: April 5th, 2017, 11:17 pm 
 

Joined: October 16th, 2016, 5:08 pm
Posts: 12
Thanks

_________________
You can't be stabbed in the back if you don't have any friends.
Why live? Cause why not.


Top 
 Profile  
Reply with quote  
 Post Posted: April 6th, 2017, 12:11 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2546
Location: Oklahoma, United States
Was wondering if that is what you wanted by your description, glad it helped.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post Posted: April 6th, 2017, 1:20 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3281
Location: Japan
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.

_________________
Chili


Top 
 Profile  
Reply with quote  
 Post Posted: April 6th, 2017, 9:12 pm 
 

Joined: May 19th, 2016, 6:48 am
Posts: 28
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/images/I/51T3NSV6NQL.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/images/I/51cWH%2BDTJaL._SX331_BO1,204,203,200_.jpg


Last edited by Byteraver on April 7th, 2017, 7:34 pm, edited 1 time in total.

Top 
 Profile  
Reply with quote  
 Post Posted: April 7th, 2017, 9:16 am 
 

Joined: April 7th, 2017, 8:33 am
Posts: 1
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.


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 7 posts ] 

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: