Thread: Pointers as parameters...HELP!
-
September 16th, 2003, 08:17 PM #1
Pointers as parameters...HELP!
Greetings all,
I am in the middle of testing a class I wrote to keep sorted strings together. However, I have run into a HUGE problem.
I have one function that inserts these strings into the classes array as a pointer:
That is a simplified version, but it illustrates my problem.Code:void sortedStrings::insert(const char* entry) { data = new char[strlen(entry)+1]; strcpy(data,entry); }
Now the real fun begins, when entry passes into the insert function, the string is still intact (e.g. if entry = "hello", I can still print that), but after the first line where strlen() is run, if I print the value of entry I get random ASCII characters "!@@!&^as@#CV" <-- like that!
My theory is it is because strlen is trying to act on the pointer entry, and not the array of chars that entry points to, but I tried running strlen with *entry to dereference the pointer and it wouldn't compile (said couldn't convert a char to a char*...I THOUGHT IT ALREADY WAS A CHAR*!!!)
Please tell me what I am doing wrong so that strlen returns an actual length without destroying the value in entry (which you SHOULDN'T be able to change anyways since it is const!)
TIA
-
September 16th, 2003, 10:45 PM #2
^_^
Talking in numbers doesn't make you smarter.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)



LinkBack URL
About LinkBacks



Reply With Quote

no option to uninstall. probably a Net10 feature.
Is It Just Me? v233893843