Thread: checking pointer to a string

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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