• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
dreamslayerx

Simple array question

7 posts in this topic

Hi guys. I hope you can help me out with this simple question. I am a beginner so try to explain it as simple as possible.

Ok this is what I want to do. I understand how to do this, but the question is why doesn't the other approach work?


How do some people do this when programming using c++?
1)
int i = 16;

char array[i] = {0........ up to 16 characters};


Ok the big thing is I under stand how to do this with a for loop. Like
2)
for(int i =0; i<16; i+=)
{
array[i] = ' some character ';
i++;
}


on 1) how is that done or can it be done at all?
0

Share this post


Link to post
Share on other sites
[quote name='dreamslayerx' timestamp='1316022481' post='4861659']
int i = 16;

char array[i] = {0........ up to 16 characters};
[/quote]
You can't do this way because; char array[size]; is allocated statically. It means that during compilation the compiler will allocate this 'size' space for you together with the program executable.
Because the compiler is not an interpreter, during compilation it does not know what value 'i' has. Therefore you must pass a real number there:

[code]
char array[10] = {0,1,2,3,4,5,6,7,8,9};
[/code]
1

Share this post


Link to post
Share on other sites
Do what?!?  [code] int i = 16; [/code] Is a single variable declaration and assignment.  [code] char array[i] = 'A'; [/code]  You write to an array named &quot;array&quot; at the location as given in &quot;i&quot;. And when you write to an char array like this you can only write a single char at once. So you can write only one integer when you have an array of integers.  With a for loop you can fill your array with whatever you want.  But I didn't see your question what do you want to know?

Edit:
Too late and now I understand it.
1

Share this post


Link to post
Share on other sites
Guys I understand how to write an array. I don't need an explanation on that. When I wrote

char array[i] = {0 ...... up to 16 characters} implies that the array is filled with just 16 characters. I wouldn't write it that way when coding. That is just to give a quick example without me writing in all 16 characters.

The question is how can you write

int i = 16;

char array[i] = {0...... up to 16 characters};


I have seen sample code, but when I do for example

int x = 10;

int array[x] = {.......Here you put in 10 integers};

It has a problem. I am wondering do I need to include a library or something to us a variable to declare my space allocation for my array to be 10 in this case.

If this way cann't be done, why do people mislead beginners by using that they of syntax when doing examples?
0

Share this post


Link to post
Share on other sites
What the first guy said is that it needs to know before hand. Your code goes into a program as such:

[program start | int i | char[0] | char[1] | int i # 2 | program end]

So when your program is put into an exe, it is a specific size. It cant just read the variable while its running and plop in char[2], char[3] etc in there. You have 2 options:

std::vector<char> array; which is a resizeable array.
array.push_back('A'); // array size is now 1

or do
array = new char[i]; // creates 'i' chars in RAM while the program is running.
1

Share this post


Link to post
Share on other sites
[quote name='dpadam450' timestamp='1316040415' post='4861766']
What the first guy said is that it needs to know before hand. Your code goes into a program as such:

[program start | int i | char[0] | char[1] | int i # 2 | program end]

So when your program is put into an exe, it is a specific size. It cant just read the variable while its running and plop in char[2], char[3] etc in there. You have 2 options:

std::vector<char> array; which is a resizeable array.
array.push_back('A'); // array size is now 1

or do
array = new char[i]; // creates 'i' chars in RAM while the program is running.
[/quote]


Yeah the problem can be easily solved by declaring a new char[variable] and have the variable to be some integer number. I just was so lost at one point time when I first saw that. To solved my problem I declared something similar to the above qoute. I was just wondering was it possible to do without going that route or putting specifying the size of the array for memory allocation.
0

Share this post


Link to post
Share on other sites
[quote name='Dragonsoulj' timestamp='1316040768' post='4861771']
Are you sure you saw that for C++? Maybe they are doing that for a 2D array?
[/quote]


Nope no 2D array. Just wanted to clerify the issue of specifying arrays. I understand how they work. Thanks for the help guys. From our discussion I am assuming that doing it in the 1) method it will not work without declaring a new array of some size to allocate the memory.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0