I need to get the size of a char array from a pointer to that array. How do I do this?
I tried to use the sizeof operator on the char pointer, but that only tells me the size of the pointer, and not the size of the array its pointing at.
I need to get the size of a char array from a pointer to that array. How do I do this?
I tried to use the sizeof operator on the char pointer, but that only tells me the size of the pointer, and not the size of the array its pointing at.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
And strlen just says how much memory is used -- there is no way, just from a pointer, to know how much memory is available on the other side (i.e., it's your job to keep track).
Yes, the array I'm using is a c-string, so its null-terminated.
So something like this then:
Code:#include <iostream> using namespace std; int main() { char array[] = "hello"; //5-character char array + null character char* pArray = array; //get a pointer to the array; int sizeOfArray = 0; while (pArray[sizeOfArray] != '\0') { //loop until NULL character is found sizeOfArray++; //increment size } cout<< sizeOfArray; }
Last edited by Programmer_P; 05-27-2010 at 06:44 PM.
Hmm...
doesn't seem to work in all cases.Code:char array[10]; int sizeOfArray = sizeof(array);
When I use a function with a type like this:
then inside that function, get the "sizeof(searchString)" it ends up outputting the size + 2. I don't know why that is.Code:bool containsString(const char* sourceString, const char searchString[]);
When I called the function, I did this:
with other test code. I noticed that it was returning the wrong value out of the function. It said it was "false" that "Ye" was contained in "Yes". Obviously that is a lie. So I then outputted the calculated size of both the strings from the function, to see if they were accurate or not. The size of the sourceString was correct (i.e. 3), but the size of the searchString wasn't. It was a 4.Code:char array[] = "Yes"; //3-character c-string bool stringIsContained = containsString(array, "Ye");
Last edited by Programmer_P; 05-27-2010 at 07:30 PM.
Is there a specific reason you are not using std::string?
Woop?
I don't actually believe you here, to be honest. If you declared your containsString function as above, then sizeof would give 4 for both, since both parameters would be of type const char*.
Again: if you are dealing with pointers, it is not possible to determine how much memory is on the other side of that pointer -- and once you pass an array to a function, all of a sudden you've got a pointer. You can find the length of the string that is pointed to with strlen, but that does not actually say how much memory is/was available.
Actually, that is not true. The "searchString" paramater is a const char array. Of course, though, maybe that's the same thing, in which case that explains why its outputting 4...
See, I thought the char searchString[] parameter would accept a char array (or literal string) passed in, and copy the contents of the passed in array to a new array which it creates on the stack with the same size as the passed-in array.
Last edited by Programmer_P; 05-27-2010 at 07:35 PM.
It is not possible to pass an array to a function in C++, since the decay rules guarantee it will decay to a pointer-to-T.
EDIT: Illustrative code:
Code:#include <typeinfo> #include <iostream> void tester(char array[4]) { char other_array[4]; if (typeid(array) == typeid(other_array)) { std::cout << "Both arrays!\n"; } else { std::cout << "Not both arrays.\n"; } } int main() { char array[4]; tester(array); return 0; }
Last edited by tabstop; 05-27-2010 at 07:38 PM.
Yes, actually, there is. I'm reading lines from a file into a char array[] buffer. So I have to pass to the "containsString" function the array (i.e. the "sourceString" paramter) as an array. If I have a string* parameter, it wont work. I could use a string object, of course, but by the time I realized that, I had already switched everything in the function to be geared to a char array, instead of a string*, and I'd rather edit a few lines rather than 50 lines.
That's not really much of a reason, to be blunt. And even if you stay with C-strings, please tell us that you didn't just rewrite strstr() from the standard library.
std::string includes .c_str(), which returns a C string. So if you need to use a C++ string as a C string, just use:
Code:string mystring; some_function_or_whatever(mystring.c_str());
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Just to clarify that point, in the function, I didn't actually use sizeof on both parameters (at least not after discovering it wouldn't work on a pointer to an array). I used a while loop to increment an int variable initialized to 0 as long as the current character of the array being pointed at wasn't a null character. That's why I got size 3 for the first parameter when I passed a c-string to it containing "Yes", and size 4 for the string passed to the second parameter which contained "Ye". But now I that I know both paramaters are basically the same thing, I'm no longer using sizeof to calculate the size of either string passed in to the function. I'm using the same method for both.
Thank you.
I'll keep that in mind if I ever need to pass a C++ string to a function expecting a C-string.
Of course the issue in my case was the variable that needs to be passed to the function starts off as a c-string, not a C++ string, and so rather than converting c-string to C++ string, then passing that, I just pass as is.
I just rewrote strstr().
C++ code - 265 lines - codepad
And it is called containsString().