...how do you get the address if you really want it?
Haven't worked with cout for a while, and furthermore i wouldn't recommend it for debugging because it's buffered and if/when a program dies, the output dies with it in the buffer.
Instead, you could use (from <stdio.h>) printf( <string template>, <unlimited number of arguments> ). Sound strange? First time i've seen it, in full detail, i ran back to cout. So I won't go into details :) To see a pointer, the code looks something like this:
- SomeType *ptr;
-
// ....
-
printf( "%d\n", ptr );
"%d\n" is the 'template' of the output; it says, "Print a decimal number and a new line". "ptr" is the number in question. That's about it.
Just thought about this: in cout, do you write
*ptr, or
ptr? If it's the first, then the poor function is working properly...
char* p =myString;
while(*p) p++;
count=p-mystring;
but this doesn't work if myString has been declared with
const char* mystring = 0;
because the compiler cannot convert const char* to char*. This strikes me as absurd. Like saylng you can't have x=3 because 3 is constant.
So if myString is a const pointer I have to grit my teeth and use strlen and strcpy. (And of course strcpy requires the source to be const.
Several points here:
1. strcpy doesn't *require* the source to be const;
const in an argument list is an indication to the compiler; it says "this function doesn't alter this parameter, so it's ok to call it for a constant." If you have a function, say, void foo( char* param ), and you try to pass it a
const char*, the compiler will protest; it is forced to maintain the integrity of any const, so it won't accept it being passed to an "unsafe" function.
2. const char* mystring = 0; This one seems bizarre. I just went in gcc and it works without any problems, both with const char* and char*. Are you declaring p as const? Because that's all I can think you doing wrong.
const char* p says: "the POINTER is constant". Essentially, you're not assigning x=3, you're assigning 3=4, which is definitely not kosher.
I don't like using the <string> library because I don't like using code I don't understand. Anyway why should Bill Gates have all the fun?
Because library functions are years old, they are definitely correct. And usually they are optimized to the umpteenth level, too...