Array Initialization

How can I put default values to all the elements in a char array.
In an array of Integers, We can use:
 
  int a[250]={0};

But the same doesn't apply to array of chars
 
char[250]={'0'};

isn't working, Please suggest the correct way to do it?

Thanks!
You use an array of char pointers, such as this:

1
2
3
4
5
6
7
8
9
10
11
char a[4] = "bleh";
char b[8] = "bleh heh"

//Array:
char *d[] = {a, b};


//print both parts of the array:
      cout<<d[0]<<endl;     //print a ("bleh")

      cout<<d[1]<<endl;     //print b ("bleh heh") 


Remember that an array always starts with 0 - that has messed up entire programs of mine not doing that.
Last edited on
Edit: Erased everything

Doing int a[250] = {0} only initializes the first index (edit: explicitly, that is). Whether it's a int or char doesn't matter.

Doing
1
2
3
4
5
6
7
8
9
int main()
{
    int c[4]={42};
    for (int i = 0; i < 4; i++)
    {
        std::cout << c[i];
    }

}

will give 42 0 0 0.
char arrays do a similar thing, except it makes each uninitialized character just null '\0'.

As in, this
1
2
3
4
5
6
7
8
int main()
{
    char c[4]={'A'};
    for (int i = 0; i < 4; i++)
    {
        std::cout << (int)c[i] << ' ';
    }
}

will give 65 0 0 0

So just use a for loop if you need to initialize everything to some value.
Last edited on
@Ganado: No doing int a[250] = {0}; initializes all 250 elements as 0, whereas doing char[250]={'0'}; only initializes the first index.

@AceDawg: that works, but then I have to type it out 250 times, as for my task I have intialize all the values in char[250] to '0'

1
2
3
char a='0';
char *d[250]={a,a,a,.........};  // this isn't really any better
char d[250]={'0','0','0'.........}; // I could've done this instead as well.  


I want to know if there's a single line code to initialize all the values to a same default char?
Did you try the code? char values are implicitly null characters '\0' in the code I posted.
If what you're saying is true, then int a[4] = {4} would make the array be {4, 4, 4, 4}. It's not, it's {4, 0, 0 ,0}.
Doing
1
2
3
4
5
6
7
8
int main()
{
    int a[100];
    for (int i = 0; i < 100; i++)
    {
        std::cout << a[i] << ' ';
    }
}

will make everything uninitialized.

Doing

1
2
3
4
5
6
7
8
int main()
{
    int a[100] = {4};
    for (int i = 0; i < 100; i++)
    {
        std::cout << a[i] << ' ';
    }
}
Initializes first element to 4, and rest to 0.


Like wise

doing
1
2
3
4
5
6
7
8
int main()
{
    char c[100];
    for (int i = 0; i < 100; i++)
    {
        std::cout << c[i] << ' ';
    }
}
Will make c have junk values.

Doing

1
2
3
4
5
6
7
8
int main()
{
    char c[100] = {'A'};
    for (int i = 0; i < 100; i++)
    {
        std::cout << c[i] << ' ';
    }
}

will make the array be {'A', '\0', '\0', '\0',...}

Anyway, imo it's bad to rely on implicit things like that, I would go through a loop either way to initialize the values. Also to answer the original point, no, there is no way to make your char array have '0' for all indexes (the character zero, not the null character '\0') without a loop.
Last edited on
@Ganado: No doing int a[250] = {0}; initializes all 250 elements as 0, whereas doing char[250]={'0'}; only initializes the first index.

Wrong.

Doing int a[250] = {0}; initializes only the a[0] with value you give within braces. The rest are zero-initialized. You don't see it because you did initialize the first element to 0 as well.

Doing char a[250] = {'0'}; does exactly the same thing; first element is initialized with the supplied value '0' and the rest are zero-initialized. The thing is that '0' is not the same as '\0'.

You could have more values in the brace list and that many elements will be initialized with those values, but if the array is larger, then the rest are zero-initialized.

Oneliner?
std::string a( 250, '0');
Oops, Sorry bout that man, I was initializing int a[250]={0}; I didn't realize initializing to any other integer value will give me the same result. I think the best way is to use loop to initialize all values one by one.
Thanks you for your time anyways!
@keskiverto: Thanks, I get it now. I'm gonna try the one-liner and see if that works.
Last edited on
Topic archived. No new replies allowed.