Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the last entry in an unfilled array (C++)?

Tags:

arrays

c#

I put C++ because I'm just starting in C# and I'm not sure if there's a difference.

if you declare an array

char arr[10] 

and fill in values for arr[0] through arr[8], what value will be put in arr[9]?

a space ' '? An endline '\n'? '\0'? Or is it nothing at all?

I'm asking this because I've always used tactics like this

char word[20];
for(count = 0 ; count < 20 ; count++)
{
  cout << word[count];
}

to print the entire contents of an array, and I was wondering if I could simplify it (e.g., if the last entry was '\0') by using something like this

char word[20];
while(word[count] != '\0')
{
  cout << word[count];
}

that way, I wouldn't have to remember how many pieces of data were entered into an array if all the spaces weren't filled up.

If you know an even faster way, let me know. I tend to make a bunch of mistakes on arrays.

like image 777
superlazyname Avatar asked Dec 03 '25 17:12

superlazyname


1 Answers

The C# syntax for constructing a character array is:

char[] arr = new char[10];

All values in the array will be initialized to '\0'.

Perhaps a List<char> would be better for your situation where you don't know how many characters you need. Another option to consider is a StringBuilder. Or use a string if you don't need mutability.

like image 167
Mark Byers Avatar answered Dec 06 '25 08:12

Mark Byers