Re: C and NULL character

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jameskuyper@verizon.net

    Re: C and NULL character

    mkeles84 wrote:
    I have a problem about NULL's.
    for example, a variable is name :
    --------------
    char name[6]="MEHMET"; /* 6 characters */
    if (strcmp(name,"M EHMET") == 0){
    printf("true");
    }else{
    printf("false") ;
    }
    I think result must bt "true" but I saw "false" on screen.
    how can I compare it. I don't want to use NULL characker after variable.
    The other responses you've received have explained why 'name' needs to
    contain a Null-Terminated Character String (NTCS). However, it
    occurred to me that you might not be aware of why creating a NTCS is
    normally not very difficult.

    For instance, the second "MEHMET" in your program causes a piece of
    memory to be allocated for an array of 7 chars, with the contents
    {'M', 'E', 'H', 'M', 'E', 'T', '\0'}. "MEHMET" itself is replaced in
    your code with the address of the first character of that array. As a
    result, it is automatically null-terminated, without you having to say
    so.

    The same thing did NOT happen with the initialization of the 'name'
    array, because you explicitly declared it as having a length of 6. As
    a result, the definition is equivalent to:

    char name[6] = {'M', 'E', 'H', 'M', 'E', 'T'};

    Note that there is no terminating null character in that array. If,
    instead, you had either explicity made it big enough to hold the
    terminating null character:

    char name[7] = "MEHMET";

    or, more simply, just left the array length unspecified:

    char name[] = "MEHMET";

    your definition would have been equivalent to:

    char name[7] = {'M', 'E', 'H', 'M', 'E', 'T', '\0'};

    Cross-posted to comp.lang.c, followups redirected there, since
    comp.lang.c is more appropriate for questions about how to use C.
  • --

    #2
    Re: C and NULL character

    jameskuyper@ver izon.net wrote:
    mkeles84 wrote:
    >I have a problem about NULL's.
    >for example, a variable is name :
    >--------------
    >char name[6]="MEHMET"; /* 6 characters */
    >if (strcmp(name,"M EHMET") == 0){
    > printf("true");
    >}else{
    > printf("false") ;
    >}
    >I think result must bt "true" but I saw "false" on screen.
    >how can I compare it. I don't want to use NULL characker after variable.
    >
    The other responses you've received have explained why 'name' needs to
    contain a Null-Terminated Character String (NTCS). However, it
    occurred to me that you might not be aware of why creating a NTCS is
    normally not very difficult.
    >
    For instance, the second "MEHMET" in your program causes a piece of
    memory to be allocated for an array of 7 chars, with the contents
    {'M', 'E', 'H', 'M', 'E', 'T', '\0'}. "MEHMET" itself is replaced in
    your code with the address of the first character of that array. As a
    result, it is automatically null-terminated, without you having to say
    so.
    >
    The same thing did NOT happen with the initialization of the 'name'
    array, because you explicitly declared it as having a length of 6. As
    a result, the definition is equivalent to:
    >
    char name[6] = {'M', 'E', 'H', 'M', 'E', 'T'};
    >
    Note that there is no terminating null character in that array. If,
    instead, you had either explicity made it big enough to hold the
    terminating null character:
    >
    char name[7] = "MEHMET";
    >
    or, more simply, just left the array length unspecified:
    >
    char name[] = "MEHMET";
    >
    your definition would have been equivalent to:
    >
    char name[7] = {'M', 'E', 'H', 'M', 'E', 'T', '\0'};
    >
    Cross-posted to comp.lang.c, followups redirected there, since
    comp.lang.c is more appropriate for questions about how to use C.
    I have a question, don't C arrays start with the first index in 0 as foo[2]
    in fact having 3 "positions" foo[0], foo[1], foo[2] ?
    So I think MEHMET could be placed inside of name[6];
    name[0] = M, name[1] = E, name[2] = H,name[3] = M,name[4] = E,name[5] = T,
    name[6]='\0'.
    Like:
    #include <stdio.h>

    int main(){
    char test[] = "ba";
    printf("%c",tes t[0]);

    return 0;
    }

    Prints b correctly...
    So IMHO this is not the problem. It is 3 AM here and there are ages I don
    touch C but I think the previous explanation is not *at least totally*
    correct.

    Comment

    • Keith Thompson

      #3
      Re: C and NULL character

      -- <vfbsilva@gmail .comwrites:
      [...]
      I have a question, don't C arrays start with the first index in 0 as foo[2]
      in fact having 3 "positions" foo[0], foo[1], foo[2] ?
      So I think MEHMET could be placed inside of name[6];
      name[0] = M, name[1] = E, name[2] = H,name[3] = M,name[4] = E,name[5] = T,
      name[6]='\0'.
      Like:
      #include <stdio.h>
      >
      int main(){
      char test[] = "ba";
      printf("%c",tes t[0]);
      >
      return 0;
      }
      >
      Prints b correctly...
      So IMHO this is not the problem. It is 3 AM here and there are ages I don
      touch C but I think the previous explanation is not *at least totally*
      correct.
      Yes, C arrays are indexed starting at 0, but given

      char name[6];

      the array "name" still has only 6 elements, numbered 0 through 5. The
      previous explanation was quite correct.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • Nick Keighley

        #4
        Re: C and NULL character

        On 22 Jul, 06:36, -- <vfbsi...@gmail .comwrote:
        jameskuy...@ver izon.net wrote:
        mkeles84 wrote:
        I have a problem about NULL's.
        for example,  a variable is name :
        --------------
        char name[6]="MEHMET"; /* 6 characters */
        if (strcmp(name,"M EHMET") == 0){
            printf("true");
        }else{
            printf("false") ;
        }
        I think result must bt "true" but I saw "false" on screen.
        how can I compare it. I don't want to use NULL characker after variable.
        >
        The other responses you've received have explained why 'name' needs to
        contain a Null-Terminated Character String (NTCS). However, it
        occurred to me that you might not be aware of why creating a NTCS is
        normally not very difficult.
        >
        For instance, the second "MEHMET" in your program causes a piece of
        memory to be allocated for an array of 7 chars, with the contents
        {'M', 'E', 'H', 'M', 'E', 'T', '\0'}.  "MEHMET" itself is replaced in
        your code with the address of the first character of that array. As a
        result, it is automatically null-terminated, without you having to say
        so.
        >
        The same thing did NOT happen with the initialization of the 'name'
        array, because you explicitly declared it as having a length of 6. As
        a result, the definition is equivalent to:
        >
                char name[6] = {'M', 'E', 'H', 'M', 'E', 'T'};
        >
        Note that there is no terminating null character in that array. If,
        instead, you had either explicity made it big enough to hold the
        terminating null character:
        >
                char name[7] = "MEHMET";
        >
        or, more simply, just left the array length unspecified:
        >
                char name[] = "MEHMET";
        >
        your definition would have been equivalent to:
        >
                char name[7] = {'M', 'E', 'H', 'M', 'E', 'T', '\0'};
        >
        Cross-posted to comp.lang.c, followups redirected there, since
        comp.lang.c is more appropriate for questions about how to use C.
        >
        I have a question, don't C arrays start with the first index in 0
        yes
        as foo[2]
        in fact having 3 "positions" foo[0], foo[1], foo[2] ?
        no. foo has two cells foo[0] and foo[1]

        So I think MEHMET could be placed inside of name[6];
        name[0] = M, name[1] = E, name[2] = H,name[3] = M,name[4] = E,name[5] = T,
        name[6]='\0'.
        no

        Like:
        #include <stdio.h>
        >
        int main(){
                char test[] = "ba";
        this is equivalent to
        char test[3] = {'b', 'a', '\0'};
        (assuming I made no typos!)
        that is the compiler automatically leaves space for the terminating
        null
        character
                printf("%c",tes t[0]);
        you need to either put a '\n' in the string
        or call fflush(stdout) to ensure the output appears
        (actually stdout is probably flushed when the program
        terminates so strictly^2 it *doesn't* need an explicit
        fflush()...)
        >
                return 0;
        >
        }
        >
        Prints b correctly...
        as it should
        So IMHO this is not the problem. It is 3 AM here and there are ages I don
        touch C but I think the previous explanation is not *at least totally*
        correct.
        the previous explanation was totally correct

        --
        Nick Keighley

        "High Integrity Software: The SPARK Approach to Safety and Security"
        Customers interested in this title may also be interested in:
        "Windows XP Home"
        (Amazon)

        Comment

        • --

          #5
          Re: C and NULL character

          Thanks a lot for the explanations I just understood it now.

          Keith Thompson wrote:
          -- <vfbsilva@gmail .comwrites:
          [...]
          >I have a question, don't C arrays start with the first index in 0 as
          >foo[2] in fact having 3 "positions" foo[0], foo[1], foo[2] ?
          >So I think MEHMET could be placed inside of name[6];
          >name[0] = M, name[1] = E, name[2] = H,name[3] = M,name[4] = E,name[5] =
          >T, name[6]='\0'.
          >Like:
          >#include <stdio.h>
          >>
          >int main(){
          > char test[] = "ba";
          > printf("%c",tes t[0]);
          >>
          > return 0;
          >}
          >>
          >Prints b correctly...
          >So IMHO this is not the problem. It is 3 AM here and there are ages I don
          >touch C but I think the previous explanation is not *at least totally*
          >correct.
          >
          Yes, C arrays are indexed starting at 0, but given
          >
          char name[6];
          >
          the array "name" still has only 6 elements, numbered 0 through 5. The
          previous explanation was quite correct.
          >

          Comment

          Working...