Thread: checking pointer to a string

  1. #1
    Stinking it up. StinkyRyan's Avatar
    Join Date
    Jun 2004
    Posts
    61

    checking pointer to a string

    how do i find if the first character of a pointer to a string doesnt equal a character:

    Code:
    void purge(char* check)
    {
    if(check[0] != "#")
    {
    check = strcat("#",check);
    }
    }
    I'm gettting
    Cannot convert 'char' to 'char *' in function purge(char *)

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    '#'

    But you have other problems I'll let other elaberate on.....(gotta go)

    gg

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    you just need to change "#" to '#' and you should be good to go
    [edit] Dang you codeplug [/edit]
    Woop?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1)
    Code:
    if(check[0] != "#")
    should be
    Code:
    if(check[0] != '#')
    2)
    Code:
    strcat("#",check);
    What are you trying to do here? "#" points to a read only area in memory so trying to append check onto it will likely cause a seg fault.

  5. #5
    Stinking it up. StinkyRyan's Avatar
    Join Date
    Jun 2004
    Posts
    61
    Ok thats gone now this is what i have

    Code:
    if(check[0] != '#')
    {
    strcat('#',check);
    }
    I'm just trying to add and # at the begining if there isnt one.

    im getting this
    Error E2285 .\main.cpp 116: Could not find a match for 'strcat(char,char *)' in function purge(char *)

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >strcat('#',check);
    That still won't work.

    >I'm just trying to add and # at the begining if there isnt one.
    Using C-style strings you prety much have no choice but to use a temporary and do things manually:
    Code:
    void
    purge(
      char *check
      )
    {
      if (*check != '#') {
        char *save = new char[strlen(check) + 2];
        strcpy(save, "#");
        strcat(save, check);
        strcpy(check, save);
        delete [] save;
      }
    }
    Using your function declaration, it doesn't really get better. The above function is dangerous and bug riddled, and you should avoid it if possible. If you can switch to C++ strings, things become much easier:
    Code:
    void
    purge(
      string& check
      )
    {
      if (check[0] != '#') {
        check = '#' + check;
      }
    }
    Or you'll just have to add parameters to the function so that the C solution can be made more safe:
    Code:
    void
    purge(
      char   *check,
      size_t  size
      )
    {
      if (*check != '#') {
        if (strlen(check) + 1 >= size) {
          throw "Error";
        }
        char *save = new char[strlen(check) + 2];
        strcpy(save, "#");
        strcat(save, check);
        strcpy(check, save);
        delete [] save;
      }
    }
    And so on...
    My best code is written with the delete key.

  7. #7
    Stinking it up. StinkyRyan's Avatar
    Join Date
    Jun 2004
    Posts
    61
    you lost me with that whole c++ string thing when did that come about that looks a hell of a lot easier, i havent been programming in like a year or so i think i am really out of date

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >when did that come about that looks a hell of a lot easier
    It is easier, and it's been an official part of the language since 1998 (though present for some time before that), not to mention the slew of string classes written by just about every C++ programmer since C with Classes.
    My best code is written with the delete key.

  9. #9
    Stinking it up. StinkyRyan's Avatar
    Join Date
    Jun 2004
    Posts
    61
    hmmm I think i remember seeing it once a while ago but every tutorial I've read it used character arrays, I read last night that that was because its not going along side the object orianted ways to use a string class like that.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >every tutorial I've read it used character arrays
    They were either old or substandard. C-style strings using null terminated character arrays are error prone.

    >I read last night that that was because its not going along side the object orianted ways to use a string class like that.
    That's bs. If use of std::string goes against OOP then C-style strings are even worse. Can you quote what you read or provide a link so I can see it in context?
    My best code is written with the delete key.

  11. #11
    Stinking it up. StinkyRyan's Avatar
    Join Date
    Jun 2004
    Posts
    61
    http://www.tacc.utexas.edu/services/...g/how_0483.htm

    i found that site after you spoke of it

    also the code you showed
    Code:
    if (check[0] != '#') {
        check = '#' + check;
      }
    compiles but doesnt work, and I can't find a way to be able to make a string into all lower case. none of the ways in the faq work with std strings
    Last edited by StinkyRyan; 06-19-2004 at 10:18 AM.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I get the impression that these people are OO cultists who feel that it isn't OO unless everything is an inheritance hierarchy. But they are correct in saying that the standard library should be used as the foundation for higher level solutions. Nowhere do they even imply that C-style strings should be used in favor of the C++ string class because it isn't object oriented.
    My best code is written with the delete key.

  13. #13
    Stinking it up. StinkyRyan's Avatar
    Join Date
    Jun 2004
    Posts
    61
    I must have read it wrong it was really late.

  14. #14
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76

    string class to lower and check char

    to change a string to lower case, go through the string one char at a time and use function: newString[i] = tolower(myString[i]);

    to check if first is a letter you could use this function:
    isalpha(myString[0])

    both these functions are in <cctype>
    given i havnt bothered to deal with the pointer stuff...
    Last edited by ryan_germain; 06-21-2004 at 11:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM