String pointer questions in C.

Discussion in 'Mac Programming' started by francis21, Feb 6, 2012.

  1. francis21, Feb 6, 2012
    Last edited: Feb 6, 2012

    macrumors member

    Joined:
    May 2, 2011
    Location:
    Mississauga, ON, Canada
    #1
    found a solution. Thanks guys! :)
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    s is a char * variable. So *s should point to a char. "" is not a char. It's a zero length string. A string is an array of characters. So this is effectively a pointer. Which is, effectively, an int. Hence on this line

    Code:
    *s = "";
    you get a warning as the type on the right is a pointer and the expected type is a character.
     
  3. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #3
    Use the code tags please.

    Also, you should really think about properly indenting your code for greatly improved readability. Lastly, your error is a punctuation error. In C, what does "" represent ? What type is *s in your normalize function ?

    Right there should tell you what you're doing wrong.

    Also, you should rethink your function to remove the return statements, they are completely unneeded.
     
  4. macrumors 6502

    Joined:
    Mar 8, 2004
    #4
    One problem is in this line:

    Code:
    if (('0' <= *s && *s <= '9') || *s == ' ') {
    The compiler is yelling at you because your char pointer (and your '0' and '9' chars) is being de-referenced and cast into an integer to use the <= operator. There should be some string/char comparison functions in the c standard library that you can use instead. *hint hint* ;).
     
  5. thread starter macrumors member

    Joined:
    May 2, 2011
    Location:
    Mississauga, ON, Canada
    #5
    So in my code above, it make sense that "" represents a char *. However, let say if I see a digit or a space, how would I represent an empty char? The following '' would not work since the compiler complains that it is an empty char.

    My goal on that part of my code is to kind of "concatenate" (not the best word to explain my goal) the strings together. So that is produces the expected output. By putting an empty string where *s points at that moment allows the other strings to be pushed over.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    I don't think so. Unless things have changed a lot since I learnt C many years ago you can use <= on chars.

    ----------

    There is no such thing as an empty char. You cannot insert such a char (as it does not exist) into the position in the string to concatenate. You need to move all the other chars one position down including the \0 terminator char.
     
  7. macrumors 6502

    Joined:
    Mar 8, 2004
    #7
    Could be a C++ thing then. I tend to backport knowledge from C++ to C and this could be one of those differences that don't match up or just a compiler specific thing. I seem to remember getting yelled at by the compiler in Microsoft VC++ (possibly because of localization/portability concerns between source files).
     
  8. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #8
    That's completely untrue. If you'd bothered to copy/paste and compile his code, you'd know that is not the line the compiler complains about.
     
  9. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #9
    Take a deck of cards. Lay out the cards in a row, side by side.

    If you want to paint all red cards in black, you can do that without moving any cards.

    But if you want to pain all red cards in black, _and_ you want to remove all aces, but without leaving gaps, you'll have to move cards around.

    Same thing here.
     
  10. thread starter macrumors member

    Joined:
    May 2, 2011
    Location:
    Mississauga, ON, Canada
    #10
    First of all, thanks for the person above who edited my code, since I just copy-and-paste my code from CodeRunner. :)

    Punctuation error regarding ""? It is kind of vague on what you mean by that. "" is an empty string (or char *). Also, I know that while going through the string s, *s currently looks at the value that s is currently pointing to.
     
  11. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    You're welcome (only Moderators and Gods can edit other people's posts). If you post more code the [ code ]code here[ /code ] (without the spaces in the tags) tags should be used. I didn't need to edit/alter it apart from the tags: copy and paste seems fine.

    Oh and I don't think "punctuation error" is really the correct terminology. You have a logic error in thinking you can set one of the characters in the string to be "blank" or "empty".
     
  12. macrumors 6502

    Joined:
    Mar 8, 2004
    #12
    Ouchies, I love you too haha. Sadly I don't have one on me 24/7 but that's not to say one can't read source code and detect an error. If I was wrong this time, oh wells. I'll let them cut my hands off/hang/draw and quarter me later.

    That said, looking at it again, I realize what compiler error I thought I had seen (and so you don't have to pull out a compiler, I already know isn't the same warning/error):

    Code:
    void normalize(std::string *s){
    ...
    		if (('0' <= *s && *s <= '9') || *s == ' ') {
    ...
    }
    
    I guess that's what happens when you spend too much time in C++ land.
     
  13. thread starter macrumors member

    Joined:
    May 2, 2011
    Location:
    Mississauga, ON, Canada
    #13
    That makes more sense. I've given it some thought and I think you just can't really place an "empty" string at that given spot where s is currently pointing at (the value I mean). So I'm guessing this requires looking "ahead" of the next characters and placing the next character found to the spot where I have encountered a digit or a space already.
     
  14. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #14
    I'm referring to "" vs ''. Quotes rather than punctuation if you don't like the word. Using '\0' would have removed his warning (which is what his original post was referring to, the warning was about converting the string pointer and placing it into a char without a cast). Of course, that wouldn't have fixed his function to do what he intended to do though.

    ----------

    Actually, I wouldn't over complicate matters with looking ahead and stuff. Just implement a simple function :

    Code:
    char * removecharacteratindex(char * string, int index);
    
    use the return value to indicate an error (null pointer) or success (the new string). Basically, you should check that index is not greater or equal than strlen() - 1, and then using some pointer trickery, just create 2 substrings by substituting index with '\0', then strncat()'ing them back together.
     

Share This Page