Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple array question

Started by dreamslayerx Sep 14, 2011 at 5:48 PM 6 replies 1.8k views
Original Post
dreamslayerx
dreamslayerx
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 = {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 = ' some character ';
i++;
}


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

int i = 16;

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

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:


char array[10] = {0,1,2,3,4,5,6,7,8,9};
Programming is an art. Game programming is a masterpiece!
megamoscha
megamoscha
Do what?!? int i = 16; Is a single variable declaration and assignment. char array = 'A'; You write to an array named "array" at the location as given in "i". 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.
dreamslayerx
dreamslayerx
Guys I understand how to write an array. I don't need an explanation on that. When I wrote

char array = {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 = {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?
dpadam450
dpadam450
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 array; which is a resizeable array.
array.push_back('A'); // array size is now 1

or do
array = new char; // creates 'i' chars in RAM while the program is running.
NBA2K, Madden, Maneater, Killing Floor, Sims 
Dragonsoulj
Dragonsoulj
Are you sure you saw that for C++? Maybe they are doing that for a 2D array?
dreamslayerx
dreamslayerx

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 array; which is a resizeable array.
array.push_back('A'); // array size is now 1

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



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.
dreamslayerx
dreamslayerx

Are you sure you saw that for C++? Maybe they are doing that for a 2D array?



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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.