Member Avatar for zacblazic

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.

what happens if string is uninitialized ?

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.

or if its empty string.

Member Avatar for zacblazic

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.

Member Avatar for zacblazic

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

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:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.