+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Senior Member squeech's Avatar
    Join Date
    May 2002
    Location
    Rocky Mountain High
    Posts
    613

    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

  2. #2
    Senior Member squeech's Avatar
    Join Date
    May 2002
    Location
    Rocky Mountain High
    Posts
    613
    ^_^
    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)

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews