• Create Account

Simple array question

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

#1xXDreamXx  Members

Posted 14 September 2011 - 11:48 AM

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?

#2kuramayoko10  Members

Posted 14 September 2011 - 12:01 PM

int i = 16;

char array[i] = {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!

#3megamoscha  Members

Posted 14 September 2011 - 12:01 PM

Do what?!?
int i = 16;
Is a single variable declaration and assignment.
char array[i] = 'A';
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.

#4xXDreamXx  Members

Posted 14 September 2011 - 04:26 PM

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?

Posted 14 September 2011 - 04:46 PM

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.

#6Dragonsoulj  Members

Posted 14 September 2011 - 04:52 PM

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

#7xXDreamXx  Members

Posted 14 September 2011 - 05:08 PM

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.

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.

#8xXDreamXx  Members

Posted 14 September 2011 - 05:10 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.