Simple pointer question

This is a discussion on Simple pointer question within the C Programming forums, part of the General Programming Boards category; Some code I'm reading contains this function: Code: char* strupr(char* str) { char *s = str; while (*s) { *s ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Simple pointer question

    Some code I'm reading contains this function:

    Code:
    char* strupr(char* str)
    {
        char *s = str;
    
        while (*s)
        {
            *s = toupper(*s);
            s += 1;
        }
        return (str);
    }
    ...and I was just wondering why the need for 's' at all, when you could just perform the same operation on 'str'?

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    If these operations were performed directly on str, it is true that the string used as the argument in the calling function would be converted to uppercase, but the char * that the function returns would point to the null char at the end of the string instead of pointing to the beginning of the string.

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    D'oh, of course....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM
  5. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21