Thread: Expecting a null character, but receiving a blank character instead

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    7

    Expecting a null character, but receiving a blank character instead

    Consider the following declaration:

    Code:
    char error_msg[15] = "";
    I call a function which essentially populates this error message with either an error message (in the case of an error), or with SPACES (if no error is encountered). For reasons I donīt need to get into in this post, I trim the string if it is returned to me as SPACES.
    In the relevant section of the code, I check for its length:

    Code:
    printf("Length of error_msg = %d\n", strlen(error_msg));
    When the error message comes back as spaces, the result of trimming the string and executing the line above is always 0. I interpret that to mean that the array is empty, and that the first element of the array is a null character (\0).
    But the string compare below always returns a 32:

    Code:
    compare_result = strcmp(error_msg, "")
    So I decided to do a printf to check the first element of the error message array:

    Code:
    printf("First element of the error msg array: %d", error_msg[0]);
    I print the character as an integer in order to see the decimal 0, which corresponds to the null character. But the result I get is the decimal 32, which corresponds to the "blank space" character. Now, if there is a blank character as the first array element, shouldnīt the array length be GREATER than 0?

    I think thereīs something Iīm missing here...
    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    strlen definitely should be counting leading spaces. A scope error (you having two variables called error_msg) is possible, especially if you're trying to cheat by using global variables. Can you give us a complete, compilable example that does bad things (rather than a line here and a line there)?

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    No, by definition when you make the string literal you are making, aka
    Code:
     char error_msg[15] = ""
    , the string will contain the space character. This will still return a string lenth of zero.

    Now if you wanted the string to be NULL to begin with you would need to change your declaration to
    Code:
    char error_msg[15]={'0'};
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    No, by definition when you make the string literal you are making, aka
    Code:
     char error_msg[15] = ""
    , the string will contain the space character. This will still return a string lenth of zero.
    Don't confuse "" with " ".


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by AndrewHunter View Post
    No, by definition when you make the string literal you are making, aka
    Code:
     char error_msg[15] = ""
    , the string will contain the space character.
    Pretty sure this is completely false (and probably irrelevant, given the description). Easy test:
    Code:
    #include <stdio.h>
    int main(void) {
        char error_msg[15] = "";
        int i;
        for (i = 0; i < 15; i++) {
            printf("%d\n", error_msg[i]);
        }
        return 0;
    }
    I typed that straight into this box and didn't run it, but I'll bet you get a bunch of 0.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You know what, scratch completely what I said. You are right Quzah. I don't even know what I was thinking.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    You know what, scratch completely what I said. You are right Quzah. I don't even know what I was thinking.
    ... and thanks to that voltage reduction on your crystal ball, neither do I...

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If Microsoft is involved the OP story could be true.

    In MS T-SQL version 6.5 and empty string had a single space in it.

    Tim S.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    Andrew: Yup, as Quzah implied, "" has no blank space, as itīs the empty string. And as the definition goes, "An empty string is a character array with the NULL character in the zeroth index position".

    Tabstop: No scope errors and no global variables. This error_msg is the only one there is. And yup, just as you predicted, we get all zeros when executing your program.

    When I get err_msg fully populated by blank spaces (when there are no errors), I said that I call a trimming function. Here it is, just for the sake of it:

    Code:
    void RTRIM (char * chain)                                      
    {
            long n;         /* Counter */
            for (n = strlen (chain); n > 0 && chain [--n] == ' '; chain [n] = '\0');
    }
    Again, after I call RTRIM, I call strlen to check the size of err_msg. The result I get is zero. Strlen returns the number of characters between the beginning of the string and the null character. So if the size I get back is 0, then the null character (\0 or 0 in decimal) must be the 0th element of the array, no? And if so, why in the world am I getting the decimal 25 (corresponding to a blank character) when I do the string compare below?

    Code:
    compare_result = strcmp(error_msg, "");
    I confess Iīm perplexed!!

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, if you write:
    Code:
    printf("%d %d\n", strcmp(error_msg, ""), (int)error_msg[0]);
    "25 0" or "32 0" but not "0 0" is printed?
    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

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    It's going to have to be something with how you populate your array with blank spaces. When I run this:
    Code:
    # include <stdio.h>
    # include <string.h>
    
    int main(void){
    
    	char myStringA[5] = {' '};
    	long n;
    
    
                 for (n = strlen (myStringA); n > 0 && myStringA[--n] == ' '; myStringA[n] = '\0');
    
    	printf("%d", strcmp(myStringA,""));
    
    	getchar();
    	return(0);
    }
    My output is 0.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. room for null character
    By jackson6612 in forum C++ Programming
    Replies: 4
    Last Post: 06-17-2011, 05:33 AM
  2. Blank Character Constant Problem
    By rrc55 in forum C Programming
    Replies: 9
    Last Post: 02-10-2009, 03:50 PM
  3. question about Null character
    By genie in forum C Programming
    Replies: 4
    Last Post: 11-06-2008, 09:28 PM
  4. null after every character!!?!
    By the bassinvader in forum C Programming
    Replies: 15
    Last Post: 09-09-2006, 07:11 PM
  5. Character Array: If Null
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:06 PM