0

Hey guys,

So I today I converted my programs from VS 2005 to VS 2010.

I came across an issue involving the string null terminator ('\0') in a function of mine. (Note: It worked in VS 2005).

int StrLength(string str)
{
	int i = 0;

	while(str[i] != '\0')
	{
		i++;
	}

	return i;
}

I get the error: 'string subscript out of range'.

My take on this is that when the index gets to the end of the string (where it should compare the null terminator) it has already jumped off the end of the string.

I'm probably wrong though.

Does anyone have any idea what to do here? Any help is appreciated.

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by Narue
0

what happens if string is uninitialized ?

Edited by Crutoy: n/a

0

How do you know str ends with '\0'? Unless you placed on there explicitly, the error is correct and you're walking off the end of the string.

0

Ah I see. I did not place it there explicitly. I just don't understand why it works in 2005 and not 2010. Thank you for the replies.

0

The string is initialized and it is not empty, but thanks guys.

0

I just don't understand why it works in 2005 and not 2010.

Clearly there were improvements, if this bug got through on 2005 yet gets caught on 2010. :icon_rolleyes:

Edited by Narue: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.