Thread: Arrays always a bit longer

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    160

    Arrays always a bit longer

    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

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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);
        
        
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    :: 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

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    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

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by skipper
    Arrays are null terminated, i.e. the last character is '\0'. In fact, they're initially filled with null characters.
    Only strings end with \0.
    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.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    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

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    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

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    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

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit or 64 bit allignment ?! gcc options??
    By mynickmynick in forum C Programming
    Replies: 3
    Last Post: 07-29-2008, 02:43 AM
  2. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 01:26 PM
  3. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  4. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM