code is an array actually |
No, it actually is a pointer.
There is no problem with the decrypt function. The decrypt function does everything I need it to in the way I want it to. |
Maybe you just haven't tested enough.
sizeof(a_pointer)
never gives you the size of the array.
As for number one 1) pass the length into Output also; I just didn't want to do that because I am shooting for modularity. |
That is modular. It's a little extra work, yeah, but it's definately modular.
I thought to use strings, but I didn't want to make it too easy on myself |
That's a silly reason, but okay.
The issue I am having is sending the returned pointer from Decrypt() into another function which will then calculate the c-string length does your statement "here is no way to tell the size of the array with just a pointer to it." hold true even using strlen? |
Well you problem here, then, is that your string isn't null terminated.
strlen just steps through the string looking for the null character. It stops when it finds it.
This is
not the same thing as returning the size of the array, since the size of the array may be larger than the actual string length (or shorter, if you screwed up and corrupted memory).
Try this out once:
1 2 3 4 5 6 7 8 9
|
char foo[100] = "A test, baby";
cout << strlen(foo) << endl; // outputs 12
cout << sizeof(foo) << endl; // outputs 100
char* ptr = foo;
cout << strlen(ptr) << endl; // outputs 12 (length of the string)
cout << sizeof(ptr) << endl; // outputs 4 or 8 (size of a pointer -- not the size of the array)
|
note that strlen works ONLY if the string ends with a null terminator.
Your 'clear_text' array does not necessarily end in a null terminator because you never gave it one. Do this:
1 2 3 4
|
clear_text[array_length] = 0; // don't forget to terminate the string
// then you can return clear_text
return clear_text;
|
But of course then:
1) you have to allocate array_length+1 characters (+1 for the null)
2) you have to fix your array_length problem (use strlen instead of sizeof)