Thread: String Functions

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    9

    Smile String Functions

    I'm trying to write some code that will allow me to read in a character array from the user. I managed to get something that will read everything (up to the specified length), including spaces. Since the code for that one function is slightly long, I tried to put it into a function. My problem is that the code inside the function doesn't read anything after the first space is put in.

    Here's the working code that I have:

    *********************************************
    #include <stdio.h>

    int main()
    {
    char cString[15] = "\0";

    printf("Please type a name:");

    int nCurrChar = 0;
    for(int i = 0; (i<sizeof(cString)) && ((nCurrChar = getchar()) !=EOF) && (nCurrChar != '\n'); i++)
    cString[i] = (char) nCurrChar;
    cString[i] = '\0';

    printf("%s\n", cString);

    return 0;
    }
    *********************************************

    Here's the code that I have that isn't working:

    *********************************************
    #include <stdio.h>
    #include <string.h>

    void ReadString(char string[]);

    int main()
    {
    printf("Please type a name:");

    char cName[15] = "\0";

    ReadString(cName);

    printf("%s", cName);

    return 0;
    }

    void ReadString(char cString[])
    {
    int nCurrChar = 0;
    for(int i = 0; (i<sizeof(cString)) && ((nCurrChar = getchar()) !=EOF) && (nCurrChar != '\n'); i++)
    cString[i] = (char) nCurrChar;
    cString[i] = '\0';
    }

    *********************************************

    I copied and pasted the code that I put into the function directly from what I had above it, so I'm not sure what's going on. Any help would be greatly appreciated. :-)

  2. #2
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    The problem isn't spaces; the problem is your use of the sizeof operator. This doesn't work on C-arrays like you might expect. A C-array is actually just a pointer, so using sizeof on it will return the size in bytes of the pointer itself, not the array it indexes. No matter your array length, sizeof of a char* will be 4 bytes, invariably tripping the first for-loop exit condition and ending input after four characters.

    You can try using the C++ string class instead, or make your own string class such as this:

    Code:
    class mystring
    {
      private:
      char *cstring;
      unsigned int length;
    
      public:
      mystring(unsigned int initlength);
      ~mystring();
      unsigned int size()  {return length;};
      char *getstring(); {return cstring;};
      void readstring();
    };
    The implementation of readstring() would be almost the same as your ReadString, but with size() replacing sizeof(). Of course, you'd have to know about constructors, destructors, and dynamic memory to do this.
    Last edited by Procyon; 09-01-2001 at 12:29 PM.

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ..or if you want to continue to use c-strings you could pass the size of the array into the function as a parameter.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    would strlen() work?
    int strlen(char* sz)
    Requires that the string is terminated with the null char and returns the length as an integer...
    zMan

  5. #5
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    No, because the strlen() of an uninitialized array will be potentially unlimited, and for an array initialized to "\0" it will be zero.

    I think zen's strategy is the simplest.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > the problem is your use of the sizeof operator. This doesn't work on C-arrays like you might expect.
    This isn't exactly true - it depends on the context of the 'array' in question as to whether it 'works' or not.

    In the first case, the array is a proper array and sizeof will give you the answer you expect. sizeof will tell you the size of an array.

    In the second case, the cString isn't an array (despite the use of [] in the declaration), it is simply a pointer (all arrays are passed as pointers to the first element). In this case, sizeof will only tell you the size of that pointer (which is 4 in most of common environments).

    > ..or if you want to continue to use c-strings you could pass the size of the array into the function as a parameter.

    Code:
    #include <stdio.h>
    #include <string.h>
    void ReadString(char string[], int size);
    int main()
    {
        printf("Please type a name:");
        char cName[15] = "\0";
        ReadString(cName,sizeof(cName));
        printf("%s", cName);
        return 0;
    }
    void ReadString(char cString[], int size)
    {
        int nCurrChar = 0;
        for(int i = 0; (i<size) && ((nCurrChar = getchar()) !=EOF) && (nCurrChar != '\n'); i++)
            cString[i] = (char) nCurrChar;
        cString[i] = '\0';
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    9

    Smile Thanks much!

    Thanks much guys! I really appreciate the help. :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Badly designed n string functions?
    By anonytmouse in forum C Programming
    Replies: 3
    Last Post: 11-01-2003, 06:16 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM