Sign in to follow this  

Why?

This topic is 4713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <iostream> //Included for strlen().

using namespace std;

int main(int argc, char** argv)
{
	//An array of 20 integers, initialized...
	int iIntArray1[20] = {1,45,65,-32,11,234,-5000,123,90,0,
				66,32,76,10,3,-450,-500,-123,-9,6};

	//An array of 20 integers, uninitialized...
	int iIntArray2[20];

	//An array of 20 integers, partially initialized...
	int iIntArray3[20] = {5};

	//A string (char array), initialized with a string constant...
	char szSomeText1[255] = "\tThis is some sample text.";

	//A string (char array), initialized with a series of chars...
	char szSomeText2[] = {'\t','T','h','i','s',' ','i','s',' ', 's','o','m','e',' ','s','a','m','p','l','e',' ','t','e',
							'x','t','.'};

	//Another initialized string…
	char szSomeText3[] = "\tThis is some sample text.";

	//A boolean array, uninitialized...
	bool bBooleans[10];

	//Get the string length of szSomeText1...
	int iLengthOfSomeText1 = strlen(szSomeText1);

	//Get the string length of szSomeText2...
	int iLengthOfSomeText2 = strlen(szSomeText2);

	//Get the string length of szSomeText3...
	int iLengthOfSomeText3 = strlen(szSomeText3);
	
	return 0;
}

Why, when using strlen, does iLengthOfSomeText2 have a length of 62, while the other two only have a length of 26? I've been trying to figure this out but I can't seem to figure it out.

Share this post


Link to post
Share on other sites
szSomeText2 is not null-terminated, so strlen is counting past the string until it finds a null character. You should add 0 or '\0' to the end to terminate it.

Share this post


Link to post
Share on other sites
strlen would output the same thing for each one.

#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
char szSomeText1[255] = "\tThis is some sample text.";
int iLengthOfSomeText1 = strlen(szSomeText1);
std::cout << iLengthOfSomeText1 << "\n";

char szSomeText2[] = {'\t','T','h','i','s',' ','i','s',' ', 's','o','m','e',' ','s','a','m','p','l','e',' ','t','e',
'x','t','.'};
int iLengthOfSomeText2 = strlen(szSomeText2);
std::cout << iLengthOfSomeText2 << "\n";

char szSomeText3[] = "\tThis is some sample text.";
int iLengthOfSomeText3 = strlen(szSomeText3);
std::cout << iLengthOfSomeText3 << "\n";

cin.get();

return 0;
}




Outputs the following:
26
26
26


I would recommend just using std::string. It's so much easier and is part of the std library. Don't feel bad about using it.

Share this post


Link to post
Share on other sites
(IIRC) Initialising character arrays as you have done for szSomeText1 and szSomeText3 (putting them in inverted commas) adds a NULL-terminating character to the end of the text string automatically (this is not necessarily at the end of the array, however). Because of the way you initialised it, szSomeText2 has no NULL-terminating character, which means strlen must have carried on counting past the end of the array until it encountered one somewhere in memory.

Share this post


Link to post
Share on other sites

This topic is 4713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this