»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 09-16-2003, 06:17 PM   #1 (permalink)
Registered User
 
squeech's Avatar
 
Join Date: May 2002
Location: Rocky Mountain High
Posts: 522
squeech is on a distinguished road
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:
Code:
void sortedStrings::insert(const char* entry)
{
 data = new char[strlen(entry)+1];
 strcpy(data,entry);
}
That is a simplified version, but it illustrates my problem.

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

squeech is offline   Reply With Quote
Old 09-16-2003, 08:45 PM   #2 (permalink)
Registered User
 
squeech's Avatar
 
Join Date: May 2002
Location: Rocky Mountain High
Posts: 522
squeech is on a distinguished road
^_^
__________________
Talking in numbers doesn't make you smarter.
squeech is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 12:52 AM.