Why is it that one always is supposed to make an array one more longer then the amount of those you are going to use?
If you have to store 64 characters then you make a array like this char Whatever[65];
(I just wan a know why it is so )
Why is it that one always is supposed to make an array one more longer then the amount of those you are going to use?
If you have to store 64 characters then you make a array like this char Whatever[65];
(I just wan a know why it is so )
Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
Hell I might even learn something
The array size cant be extended in standard C++.
If you want a better array, then learn to use vectors...they are like smart arrays
Code:#include <iostream> #include <vector> void TimesTwo(std::vector<int>& vec){ std::vector<int>::iterator p;//can use iterators for(p = vec.begin();p != vec.end();++p){ *p *= 2; std::cout << *p << " "; } std::cout << std::endl; } void AddTen(std::vector<int>& vec){ for(int i = 0;i < vec.size();++i){ vec[i] += 10;//can address like array as well! std::cout << vec[i] << " "; } std::cout << std::endl; } int main() { std::vector<int> vec; vec.push_back(10); vec.push_back(2); vec.push_back(100); vec.push_back(50);//grows as you keep pushing on variables TimesTwo(vec); AddTen(vec); }
Arrays are null terminated, i.e. the last character is '\0'. In fact, they're initially filled with null characters.
Built-in arrays have no other way of "enforcing" their limits than to search for a '\0' during a 'read' operation. If none is found, it will read right on past the limit of your array until it finds one. This is not a good thing!
This is why you must be very careful to ensure that, at the very least, the last character in your array is '\0'.
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
:: Looks back at orginal post ::
Oh...and if its specifically char arrays (string) that ypour interested in then use std::string....that allows you to increase the size too
What is the difference betwem using end() and size() ?
Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
Hell I might even learn something
Only strings end with \0.Originally posted by skipper
Arrays are null terminated, i.e. the last character is '\0'. In fact, they're initially filled with null characters.
And the initialization of the array is compiler dependent. On mine arrays only contain garbage by default.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
>What is the difference betwem using end() and size() ?
end() returns an iterator to one past the last element in a container. size() returns the number of items in the container.
-Prelude
My best code is written with the delete key.
sorry but I really don't know what a "iterator" is
Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
Hell I might even learn something
>sorry but I really don't know what a "iterator" is
An iterator is an object with pointer-like abilities that references a certain spot in the container. Basically, if a container is a collection of items, an iterator to the container is a thing (technical term) that points to a single item in the collection.
-Prelude
My best code is written with the delete key.
Thx.
When we are to it () then how come that an ordinary array don't have an '\0' because how do "it" then know were to end.
Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
Hell I might even learn something
>then how come that an ordinary array don't have an '\0' because how do "it" then know were to end.
Regular arrays have no idea where they end, the programmer has to make sure that they don't access indices outside of the arrays boundaries. The nul terminator is only for C strings so that you don't always have to pass a size to stop at when you want to work with them or print them.
-Prelude
My best code is written with the delete key.
Is it then true what skipper says that the hole array is initialised with '\0'?
Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
Hell I might even learn something
>Is it then true what skipper says that the hole array is initialised with '\0'?
Only if it's a character array declared in global scope or with static linkage. Otherwise the initial values are indeterminate.
-Prelude
My best code is written with the delete key.
Sorry. Char arrays are what I was referring to but I can't, in good conscience, claim that I was thinking "universally" on this one.
Thanks to both Magos and Prelude for covering my back.
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow