Why?

Started by
2 comments, last by The Rug 19 years, 3 months ago

#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.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
Advertisement
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.
Ra
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:
262626


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.
Rob Loach [Website] [Projects] [Contact]
(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.
the rug - funpowered.com

This topic is closed to new replies.

Advertisement