I was trying to check what arrays are initialized to by default. In my case for the code below the string array was initialized to white spaces because this is what I got. Would you please tell me what arrays (C-strings are also arrays) are initialized to by default. Thanks for the help.

Code:

// learning_arrays_strings.cpp
// intializtion of an array

#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

const int C = 5;
char str[C];

int main()

{
int i;

for (i=0; i<C; i++)
{
cout << str[i];
}

cout << endl;

system("pause");
return 0;
}

06-11-2011

tabstop

You should be careful what you mean by white spaces. If you think you mean the character you get by pushing the big bar at the bottom of your keyboard, then that's not it.

All variables (arrays or no) that are declared at file scope (i.e. outside main) are initialized to 0 (that's the character 0, sometimes spelled '\0', not the character '0' which is 48). Any variables declared inside main (arrays or no) are not initialized to anything unless you do it explicitly.

06-16-2011

jackson6612

Quote:

Originally Posted by tabstop

You should be careful what you mean by white spaces. If you think you mean the character you get by pushing the big bar at the bottom of your keyboard, then that's not it.

All variables (arrays or no) that are declared at file scope (i.e. outside main) are initialized to 0 (that's the character 0, sometimes spelled '\0', not the character '0' which is 48). Any variables declared inside main (arrays or no) are not initialized to anything unless you do it explicitly.

Thanks, tabstop.

Okay. White space is not the space which doesn't contain anything. But the space one get by pressing space bar is different. It determines the space between two outputs.

The output for the code I mentioned in the my first post is:

Code:

Press any key to continue . . .

You see the white space I get. If the array elements were initialized to zero then why am I getting spaces? Please let me know. Thanks a lot.

06-16-2011

laserlight

Quote:

Originally Posted by jackson6612

You see the white space I get.

I don't.

06-16-2011

tabstop

Yeah, that looks like no spaces at all to me. The "Press any key to continue..." is printed by your system("pause").

06-16-2011

jackson6612

1 Attachment(s)

Quote:

Originally Posted by laserlight

I don't.

Hi laserlight

How could you see white spaces? You could only observe them. I'm sorry for my bad choice of words.

EDIT: Please have a look on the attached screenshot. You see there is blank space appearing above "Press any key to continue ...".

Both var and str[C] have been declared globally. I get "0" for var but for str[C] I get spaces. Why is so?

As C=15 therefore I was expecting to get series of 14 zeroes (14 and not 15 because 15th element of the array would contain NULL).

I hope you understand where I'm having difficulty. Thank for the help.

Code:

// learning_arrays_strings.cpp
// intializtion of an array

#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

int var;
const int C = 15;
char str[C];char name[C];

int main()

{
int i;

cout << var << "hello there" << endl;

for (i=0; i<C; i++)
{
cout << str[i];
}

cout << "hello there" << endl;

cout << "enter your name: ";
cin.get(name, C);

cout << name << endl;

system("pause");
return 0;
}

Code:

0hello therehello there
enter your name:

06-16-2011

tabstop

Quote:

Originally Posted by jackson6612

I get "0" for var but for str[C] I get spaces. Why is so?

Because, still, you don't get spaces. There is probably some weird C++ thing about << and characters that is going on here. (<< and characters are always a little weird, IMO.) Change the loop to just

Code:

cout << str;

and you will see nothing print.

06-16-2011

jackson6612

Quote:

Originally Posted by tabstop

Because, still, you don't get spaces. There is probably some weird C++ thing about << and characters that is going on here. (<< and characters are always a little weird, IMO.) Change the loop to just

Code:

cout << str;

and you will see nothing print.

Hi tabstop

Thank you for the help. I'm happy that my question wasn't that much silly this time.

1: If those are not spaces, then what are they? Could you please tell me?

2: Within a loop I have to have some kind of index so that looping can go on. Changing it to just " cout << str; " would obviously do nothing because there is no index variable such as "i". What were you trying to teach me then?!

3: I have a major test tomorrow so please help me with this and please don't get frustrated. Does the compiler automatically insert NULL character at the end array of char type which is a C-string?

Code:

char name[40];

cout << "enter your name";
cin.getline(name, 40);

cout << name << endl;

Suppose my name is "Jackson Heights" which is 15 characters long, then would the compiler automatically insert NULL terminator at 16th position or would it insert the NULL at 40th position?

Thanks a lot for your time and help.

06-16-2011

tabstop

1. I have no idea what it's trying to print. There's probably some sort of conversion going on.
2. Since you're supposed to take the loop out, that's pretty irrelevant.
3. Yes. The \0 character always goes at the end of the input. It would be really bad for your string to be "Jackson Heights▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀".

06-16-2011

jackson6612

Quote:

Originally Posted by tabstop

1. I have no idea what it's trying to print. There's probably some sort of conversion going on.
2. Since you're supposed to take the loop out, that's pretty irrelevant.
3. Yes. The \0 character always goes at the end of the input. It would be really bad for your string to be "Jackson Heights▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀".