Thread: Initialising Arrays

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    26

    Initialising Arrays

    I'd like to know if it's possible to initialize arrays while defining it? The same with pointers. Is it possible to automatically allocate it, like calling ptr = new int ?
    More than just saving some syntax, I have a class in whose constructor, a global array is modified. So unless it's all already initialised and set to 0, I'll have problems.
    If this can't be done, then could you tell me how to get rid of the junk that is there when the array is initialized, and not affect the data that has been put into the array, when I call the constructor (via assigning a class variable)?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by studiesrule
    I'd like to know if it's possible to initialize arrays while defining it?
    yes
    Code:
    int a[5] = {0,1,2,3,4};
    Quote Originally Posted by studiesrule
    The same with pointers. Is it possible to automatically allocate it, like calling ptr = new int ?
    yes

    Quote Originally Posted by studiesrule
    I have a class in whose constructor, a global array is modified. So unless it's all already initialised and set to 0, I'll have problems.
    Constructor is a good place to do things like this.
    Use some class for your global array and do stuff you need to initialize array in its constructor.

    Quote Originally Posted by studiesrule
    If this can't be done, then could you tell me how to get rid of the junk that is there when the array is initialized, and not affect the data that has been put into the array, when I call the constructor (via assigning a class variable)?
    the good old memset is already not working?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A vector is usually a better idea than an array anyway, and if you use a vector you can initialize it to all 0's in its constructor.
    Code:
    std::vector<int> arr(20);
    If you have to stick to arrays, you can initialize it to 0's when you define it depending on the type. Is it an array of ints?
    Code:
    int arr[20] = { 0 };

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> you must "turn that off"
    There's no reason you must turn that off. It's not even necessary in most cases.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by vart
    the good old memset is already not working?
    This does not guarantee useful null pointer values or floating-point zero values.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Wow, I'm overwhelmed by the superfast reply (10 minutes). I needed something like int arr[20] = 0. I'm pretty sure I can do the same with ptrs, and initializing them to NULL. Thanks a lot.

    Though this is unrelated, I'd like to know how to use escape sequences to variables in C++. In C, I would just use %d, or %f. For example the below code (which just prints %d 20 times):

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int i,a[20]={0};
    	for (i=0;i<=19;i++)	{
    		cout<<"%d ", a[i];
    	}
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What do you mean by escape values? There is no need for format strings with cout. Just output the variable:
    Code:
    cout << a[i];
    Your C++ book should probably have shown how to do that in an example.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Ok here it's very simple, but suppose I'd like to print lets say 5-6 variables in a line, then I'd have to do cout <<var1<<" "<<var2<<" "... I find this tedious, and irritating. It's also hard to nicely format the output.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I find this tedious, and irritating.
    Bummer.

    >It's also hard to nicely format the output.
    It's hard to concisely format the output. The iostreams library supports modifiers for formatting (which are briefly looked at in the FAQ).
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Ok thanks. I wonder why they removed this small feature from C++

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I wonder why they removed this small feature from C++
    They didn't. You can still use printf if you want. However, format modifiers in a string are error prone and very difficult to make type safe. Add to that the awful hack of variable length argument lists and the difficulty of extending the modifiers for user-defined types, and it's easy to see why iostreams don't use them.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    printf is sloooow. And they never removed it (that's one of my beefs though, they never remove anything ), they just shifted all of it into <iomanip>.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by jafet
    printf is sloooow. And they never removed it (that's one of my beefs though, they never remove anything ), they just shifted all of it into <iomanip>.
    QoI, nothing more. Nothing to do with a language -- other than correct implementation of I/O.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    I don't mean the printf feature (which I do know exists, but for backward compatibility). I was wondering why cout doesn't have a similar thing.

  15. #15
    Registered User
    Join Date
    Jun 2006
    Posts
    13
    printf is way faster than cout... so don't complain about it's speed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seperating initialising and printing of arrays into functions
    By Tom Bombadil in forum C Programming
    Replies: 1
    Last Post: 03-11-2009, 11:47 AM
  2. Declaring and initialising constant nested arrays
    By officedog in forum C Programming
    Replies: 6
    Last Post: 10-28-2008, 09:55 AM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM