set char to empty

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Magix

    #1

    set char to empty

    Hi,

    How do I set the char [ ] to empty ?

    let say I have,
    char test[20];

    can I do like this :
    *test = NULL;

    Thanks.


  • Martin Ambuhl

    #2
    Re: set char to empty

    Magix wrote:
    [color=blue]
    > Hi,
    >
    > How do I set the char [ ] to empty ?
    >
    > let say I have,
    > char test[20];[/color]

    char test[20] = "";
    if you want to initialize it to an empty string.
    [color=blue]
    > can I do like this :
    > *test = NULL;[/color]

    *test (or test[0]) is a char.
    NULL is a pointer.
    Thou shalt not assign pointer values to chars.

    *test = 0;
    test[0] = 0;

    or any of several synonymous statements will do.

    Comment

    • Barry Schwarz

      #3
      Re: set char to empty

      On Mon, 2 Aug 2004 12:50:39 +0800, "Magix" <magix@asia.com > wrote:
      [color=blue]
      >Hi,
      >
      >How do I set the char [ ] to empty ?
      >
      >let say I have,
      >char test[20];[/color]

      You cannot set an array to empty. test is an array of 20 char. Each
      of those 20 has a value (even if you don't know what the value is).

      You can set the array to an empty (strlen = 0) string by setting
      test[0] to '\0'. There are several ways to do this:

      As part of the definition
      char test[20] = "";
      char test[20] = {'\0'};

      Via an assignment
      test[0] = '\0';

      Via a function
      strcpy(test,"") ;
      memset(test,'\0 ',sizeof test);
      [color=blue]
      >
      >can I do like this :
      >*test = NULL;[/color]

      No. You cannot place an array on the left of the assignment operator.


      <<Remove the del for email>>

      Comment

      • Emmanuel Delahaye

        #4
        Re: set char to empty

        Magix wrote on 02/08/04 :
        [color=blue]
        > How do I set the char [ ] to empty ?
        >
        > let say I have,
        > char test[20];
        >
        > can I do like this :
        > *test = NULL;[/color]

        Where did you get that? Certainely not from a good C-book. 'test' being
        an array, it's a constant pointer. It can't be modified.

        What do you mean by 'empty'? An object has always a value. It is never
        'empty'.

        That said, a peculiar value could have the semantics of 'empty'. Its a
        matter of convention. If you are talking about a string, it's
        considered 'empty' or "" when you have a 0 at index 0.

        char test[20];

        test[0] = 0;

        or
        *test = 0;

        or

        strcpy (test, "");

        because "" is hard coded

        char anonymous[] = {0};

        --
        Emmanuel
        The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

        "C is a sharp tool"

        Comment

        • Magix

          #5
          Re: set char to empty


          "Martin Ambuhl" <mambuhl@earthl ink.net> wrote in message
          news:2n60itFsaj 2rU1@uni-berlin.de...[color=blue]
          > Magix wrote:
          >[color=green]
          > > Hi,
          > >
          > > How do I set the char [ ] to empty ?
          > >
          > > let say I have,
          > > char test[20];[/color]
          >
          > char test[20] = "";
          > if you want to initialize it to an empty string.
          >[color=green]
          > > can I do like this :
          > > *test = NULL;[/color]
          >
          > *test (or test[0]) is a char.
          > NULL is a pointer.
          > Thou shalt not assign pointer values to chars.
          >
          > *test = 0;
          > test[0] = 0;
          >
          > or any of several synonymous statements will do.
          >[/color]

          I used test as string buffer. After strcat to test buffer in this process, i
          will need to clear the buffer, and strcat another info to test buffer in
          another process.


          Comment

          • Richard Bos

            #6
            Re: set char to empty

            Martin Ambuhl <mambuhl@earthl ink.net> wrote:
            [color=blue]
            > Magix wrote:
            >[color=green]
            > > can I do like this :
            > > *test = NULL;[/color]
            >
            > *test (or test[0]) is a char.
            > NULL is a pointer.
            > Thou shalt not assign pointer values to chars.
            >
            > *test = 0;
            > test[0] = 0;
            >
            > or any of several synonymous statements will do.[/color]

            In particular, if you want to make it obvious that you're assigning a
            char, not an int,

            *test='\0';
            or
            test[0]='\0';

            is completely equivalent, but looks more char-ish.

            Richard

            Comment

            • Joona I Palaste

              #7
              Re: set char to empty

              Emmanuel Delahaye <emdel@yourbran oos.fr> scribbled the following:[color=blue]
              > Magix wrote on 02/08/04 :[color=green]
              >> How do I set the char [ ] to empty ?
              >>
              >> let say I have,
              >> char test[20];
              >>
              >> can I do like this :
              >> *test = NULL;[/color][/color]
              [color=blue]
              > Where did you get that? Certainely not from a good C-book. 'test' being
              > an array, it's a constant pointer. It can't be modified.[/color]

              He's modifying *test, not test. And arrays are not pointers.
              [color=blue]
              > What do you mean by 'empty'? An object has always a value. It is never
              > 'empty'.[/color]
              [color=blue]
              > That said, a peculiar value could have the semantics of 'empty'. Its a
              > matter of convention. If you are talking about a string, it's
              > considered 'empty' or "" when you have a 0 at index 0.[/color]

              This is true.

              (Sample code snipped)

              --
              /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
              \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
              "We sorcerers don't like to eat our words, so to say."
              - Sparrowhawk

              Comment

              • Emmanuel Delahaye

                #8
                Re: set char to empty

                Joona I Palaste wrote on 02/08/04 :[color=blue]
                > Emmanuel Delahaye <emdel@yourbran oos.fr> scribbled the following:[color=green]
                >> Magix wrote on 02/08/04 :[color=darkred]
                >>> How do I set the char [ ] to empty ?
                >>>
                >>> let say I have,
                >>> char test[20];
                >>>
                >>> can I do like this :
                >>> *test = NULL;[/color][/color]
                >[color=green]
                >> Where did you get that? Certainely not from a good C-book. 'test' being
                >> an array, it's a constant pointer. It can't be modified.[/color]
                >
                > He's modifying *test, not test. And arrays are not pointers.[/color]

                Dammned. I missed the '*'...

                --
                Emmanuel
                The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

                "C is a sharp tool"

                Comment

                • Magix

                  #9
                  Re: set char to empty


                  "Emmanuel Delahaye" <emdel@YOURBRAn oos.fr> wrote in message
                  news:mn.12137d4 83a544452.15512 @YOURBRAnoos.fr ...[color=blue]
                  > Magix wrote on 02/08/04 :
                  >[color=green]
                  > > How do I set the char [ ] to empty ?
                  > >
                  > > let say I have,
                  > > char test[20];
                  > >
                  > > can I do like this :
                  > > *test = NULL;[/color]
                  >
                  > Where did you get that? Certainely not from a good C-book. 'test' being
                  > an array, it's a constant pointer. It can't be modified.
                  >
                  > What do you mean by 'empty'? An object has always a value. It is never
                  > 'empty'.
                  >[/color]
                  [color=blue]
                  > That said, a peculiar value could have the semantics of 'empty'. Its a
                  > matter of convention. If you are talking about a string, it's
                  > considered 'empty' or "" when you have a 0 at index 0.
                  >
                  > char test[20];
                  >
                  > test[0] = 0;
                  >
                  > or
                  > *test = 0;
                  >
                  > or
                  >
                  > strcpy (test, "");
                  >
                  > because "" is hard coded
                  >
                  > char anonymous[] = {0};
                  >
                  > --
                  > Emmanuel
                  > The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
                  >
                  > "C is a sharp tool"
                  >[/color]


                  Basical, I used it as string buffer. I want to clear the buffer for use of
                  another process.

                  let say:
                  char test[20];

                  strcat(test,"AB C");
                  ...some routine
                  strcat(test, "DEF");
                  .... some routine
                  .... output test
                  // clear test buffer for another process
                  *test=0;
                  strcat(test, "GHI")
                  ... some routine
                  strcat(test, "XYZ")
                  ... output test


                  Comment

                  • Emmanuel Delahaye

                    #10
                    Re: set char to empty

                    Emmanuel Delahaye wrote on 02/08/04 :[color=blue]
                    > Magix wrote on 02/08/04 :
                    >[color=green]
                    >> How do I set the char [ ] to empty ?
                    >>
                    >> let say I have,
                    >> char test[20];
                    >>
                    >> can I do like this :
                    >> *test = NULL;[/color]
                    >
                    > Where did you get that? Certainely not from a good C-book. 'test' being an
                    > array, it's a constant pointer. It can't be modified.[/color]

                    FOrget that. I missed the '*'. That said, you should use 0 or '\0'
                    instead of NULL that has only a meaning in a pointer context.

                    --
                    Emmanuel
                    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

                    "C is a sharp tool"

                    Comment

                    • Emmanuel Delahaye

                      #11
                      Re: set char to empty

                      Magix wrote on 02/08/04 :
                      [color=blue]
                      > Basical, I used it as string buffer. I want to clear the buffer for use of
                      > another process.
                      >
                      > let say:
                      > char test[20];
                      >
                      > strcat(test,"AB C");[/color]

                      Stop! You can't use strcat() on an uninitialized string.

                      add

                      *test = 0;

                      before the first strcat() call, or use strcpy() the first time...
                      [color=blue]
                      > ...some routine
                      > strcat(test, "DEF");
                      > ... some routine
                      > ... output test
                      > // clear test buffer for another process
                      > *test=0;[/color]

                      Ok.
                      [color=blue]
                      > strcat(test, "GHI")
                      > ... some routine
                      > strcat(test, "XYZ")
                      > ... output test[/color]

                      Ok. Just be sure that you don't overflow the string. strncat() can
                      help.

                      --
                      Emmanuel
                      The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

                      "C is a sharp tool"

                      Comment

                      • Martin Ambuhl

                        #12
                        Re: set char to empty

                        Magix wrote:
                        [color=blue]
                        > "Martin Ambuhl" <mambuhl@earthl ink.net> wrote in message
                        > news:2n60itFsaj 2rU1@uni-berlin.de...
                        >[color=green]
                        >>Magix wrote:
                        >>
                        >>[color=darkred]
                        >>>Hi,
                        >>>
                        >>>How do I set the char [ ] to empty ?
                        >>>
                        >>>let say I have,
                        >>>char test[20];[/color]
                        >>
                        >> char test[20] = "";
                        >>if you want to initialize it to an empty string.
                        >>
                        >>[color=darkred]
                        >>>can I do like this :
                        >>>*test = NULL;[/color]
                        >>
                        >> *test (or test[0]) is a char.
                        >> NULL is a pointer.
                        >> Thou shalt not assign pointer values to chars.
                        >>
                        >> *test = 0;
                        >> test[0] = 0;
                        >>
                        >>or any of several synonymous statements will do.
                        >>[/color]
                        >
                        >
                        > I used test as string buffer. After strcat to test buffer in this process, i
                        > will need to clear the buffer,[/color]

                        No, you don't. You just need to mark it with an immediate end-of-string
                        '\0'.
                        [color=blue]
                        > and strcat another info to test buffer in
                        > another process.[/color]

                        For which my above suggestions will work just fine.

                        Comment

                        • Magix

                          #13
                          Re: set char to empty


                          "Emmanuel Delahaye" <emdel@YOURBRAn oos.fr> wrote in message
                          news:mn.12447d4 880efb036.15512 @YOURBRAnoos.fr ...[color=blue]
                          > Magix wrote on 02/08/04 :
                          >[color=green]
                          > > Basical, I used it as string buffer. I want to clear the buffer for use[/color][/color]
                          of[color=blue][color=green]
                          > > another process.
                          > >
                          > > let say:
                          > > char test[20];
                          > >
                          > > strcat(test,"AB C");[/color]
                          >
                          > Stop! You can't use strcat() on an uninitialized string.
                          >
                          > add
                          >
                          > *test = 0;
                          >
                          > before the first strcat() call, or use strcpy() the first time...
                          >[color=green]
                          > > ...some routine
                          > > strcat(test, "DEF");
                          > > ... some routine
                          > > ... output test
                          > > // clear test buffer for another process
                          > > *test=0;[/color]
                          >
                          > Ok.
                          >[color=green]
                          > > strcat(test, "GHI")
                          > > ... some routine
                          > > strcat(test, "XYZ")
                          > > ... output test[/color]
                          >
                          > Ok. Just be sure that you don't overflow the string. strncat() can
                          > help.
                          >
                          > --
                          > Emmanuel
                          > The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
                          >
                          > "C is a sharp tool"
                          >[/color]

                          I initialise:
                          char test[20]="";

                          The first print out, I got ABCDEF
                          The second print out, I got GHIXYZABCDEF, which I expect to get GHIXYZ
                          Why ?


                          Comment

                          • Emmanuel Delahaye

                            #14
                            Re: set char to empty

                            Magix wrote on 02/08/04 :
                            [color=blue]
                            > I initialise:
                            > char test[20]="";[/color]

                            Good practice. Ok.
                            [color=blue][color=green][color=darkred]
                            >>> strcat(test,"AB C");[/color][/color][/color]
                            [color=blue][color=green][color=darkred]
                            >>> ...some routine
                            >>> strcat(test, "DEF");
                            >>> ... some routine
                            >>> ... output test
                            >>> // clear test buffer for another process
                            >>> *test=0;[/color]
                            >>[color=darkred]
                            >>> strcat(test, "GHI")
                            >>> ... some routine
                            >>> strcat(test, "XYZ")
                            >>> ... output test[/color][/color][/color]
                            [color=blue]
                            > The first print out, I got ABCDEF[/color]

                            Ok.
                            [color=blue]
                            > The second print out, I got GHIXYZABCDEF, which I expect to get GHIXYZ
                            > Why ?[/color]

                            Because something wrong probably occured in 'some routine'.

                            --
                            Emmanuel
                            The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

                            "C is a sharp tool"

                            Comment

                            • Richard Tobin

                              #15
                              Re: set char to empty

                              In article <mn.136d7d48e2f 54434.15512@YOU RBRAnoos.fr>,
                              Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:
                              [color=blue][color=green][color=darkred]
                              >>>> // clear test buffer for another process
                              >>>> *test=0;
                              >>>
                              >>>> strcat(test, "GHI")
                              >>>> ... some routine
                              >>>> strcat(test, "XYZ")
                              >>>> ... output test[/color][/color][/color]
                              [color=blue][color=green]
                              >> The second print out, I got GHIXYZABCDEF, which I expect to get GHIXYZ
                              >> Why ?[/color]
                              >
                              >Because something wrong probably occured in 'some routine'.[/color]

                              Although in theory undefined behaviour could do anything, it's hard to
                              see how in practice a string could end "DEF" immediately after
                              strcat(test, "XYZ").

                              What exactly is the output test? Just a printf?

                              -- Richard

                              Comment

                              Working...