Thread: strings Vs. Char pointers

  1. #46
    Registered User
    Join Date
    Feb 2008
    Posts
    1
    Quote Originally Posted by aijazbaig1 View Post
    Hello,
    heres the code:

    Code:
    ...
    void set2caps(char *str_hld)
    
    {
        unsigned int chr_cnt = 0;
        char temp;
    
        while(str_hld[chr_cnt]) {
            if (!isupper(str_hld[chr_cnt]))
                temp = toupper(str_hld[chr_cnt]);
            str_hld[chr_cnt] = temp;
            chr_cnt++;
        }
    }
    Actually, there's a bug in this function. It should be changed as follows:

    Code:
    void set2caps(char *str_hld)
    {
        unsigned int chr_cnt = 0;
        char temp;
    
        while(str_hld[chr_cnt]) 
        {
            if (!isupper(str_hld[chr_cnt]))
            {
                temp = toupper(str_hld[chr_cnt]);
                str_hld[chr_cnt] = temp;
             }
             chr_cnt++;
        }
    }
    Otherwise, all letters that were previously capitalized are either ignored (temp is not initialized) or set to the previous uncapitalized letter.

  2. #47
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    On second thought, it did not make sense: a char* was not a pointer to an array of chars, but a pointer to a char. Then I realised that you were posting C++ code in the C programming forum, and yet used a C-style cast. Your code actually uses a reinterpret_cast, hence you mistook a char[][3] for a char*.
    I can't be bothered with C++ casts. IMHO, they're too long to type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #48
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    IMHO, they're too long to type.
    That's the point - make them easier to spot, and discourage their use. Anyway, I was just saying what the cast actually would be in C++, we cannot use it here anyway since this is C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #49
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    I can't be bothered with C++ casts. IMHO, they're too long to type.
    I ONLY use C++ casts (in C++ at least).
    C++ casts allow the compiler to tell you if you're doing something wrong (like removing const when all you wanted to do is cast the type).
    They also allow you to easily search & find any casts in your code, so you can remove them later if you change your code to make them unnecessary. Try searching for a C cast -- not quite as easy, especially if different coding styles are used.
    They make the intent of your cast obvious to everyone that reads it because each C++ cast does only one job. With C casts, people don't know if you actually intended to remove const or if it's a bug...

  5. #50
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I feel confident in C-style casts and I don't do a lot of mistakes in removing things such as "const."
    But either way, if a bug is due to a cast, I fix it pretty quickly, so C-style works for me. Not that it's good practice...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying pointers
    By Tom Bombadil in forum C Programming
    Replies: 10
    Last Post: 05-22-2009, 01:26 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM