Querry aoubt struct including character strings pointer

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

    #1

    Querry aoubt struct including character strings pointer

    Hello, all.
    If a struct contains a character strings, there are two methods to
    define the struct, one by character array, another by character pointer.
    E.g,
    //Program for struct includeing character strings, test1.c

    #include <stdio.h>
    #define LEN 20

    struct info {
    char first[LEN];
    char last[LEN];
    int age;
    };

    struct pinfo {
    char * first;
    char * last;
    int age;
    };

    int main(void)
    {
    struct info one = {"Opw", "Cde", 22};
    struct pinfo two = {"Tydu", "Gqa", 33};
    printf("%s %s is %d years old.\n", one.first, one.last, one.age);
    printf("%s %s is %d years old.\n", two.first, two.last, two.age);
    return 0;
    }

    //end test1.c

    It can work.I know a little,but not very detail that the second struct pinfo is not good
    in the above code. E.g, if the struct pinfo two is defined as an array
    two[1] in the main function, it can not work. I just want to know the
    reason in detail.

    The better way to avoid this problem is malloc() and another pointer
    point to the struct, and copy the strings, then free it. Is it right?

    Thank you.

    bowderyu



  • Ian Collins

    #2
    Re: Querry aoubt struct including character strings pointer

    bowlderyu wrote:
    Hello, all.
    If a struct contains a character strings, there are two methods to
    define the struct, one by character array, another by character pointer.
    E.g,
    //Program for struct includeing character strings, test1.c
    >
    #include <stdio.h>
    #define LEN 20
    >
    struct info {
    char first[LEN];
    char last[LEN];
    int age;
    };
    >
    struct pinfo {
    char * first;
    char * last;
    int age;
    };
    >
    int main(void)
    {
    struct info one = {"Opw", "Cde", 22};
    struct pinfo two = {"Tydu", "Gqa", 33};
    printf("%s %s is %d years old.\n", one.first, one.last, one.age);
    printf("%s %s is %d years old.\n", two.first, two.last, two.age);
    return 0;
    }
    >
    //end test1.c
    >
    It can work.I know a little,but not very detail that the second struct pinfo is not good
    in the above code. E.g, if the struct pinfo two is defined as an array
    two[1] in the main function, it can not work. I just want to know the
    reason in detail.
    >
    It will work fine.

    The answer to your question depends on your definition of "not good".
    The first form is OK if you want mutable strings with a known maximum
    length. The second is OK if you want constant strings and would be
    safer if changed to

    struct pinfo {
    const char * first;
    const char * last;
    int age;
    };
    The better way to avoid this problem is malloc() and another pointer
    point to the struct, and copy the strings, then free it. Is it right?
    >
    Which problem are you trying to avoid?

    --
    Ian Collins

    Comment

    • bowlderyu

      #3
      Re: Querry aoubt struct including character strings pointer

      Ian Collins <ian-news@hotmail.co mwrites:
      const char *, good sugesstion.

      I don't know why it can not work for the case of two[1], so I want to
      avoid using char array. But now, it seems fine.

      Would you please give more explanation about it?
      I mean for why not safe for non-constant strings.

      I am thinking about, seems clearly.

      Now, I have to leave.

      Anyway, thank you .
      bowlderyu wrote:
      >Hello, all.
      >If a struct contains a character strings, there are two methods to
      >define the struct, one by character array, another by character pointer.
      >E.g,
      >//Program for struct includeing character strings, test1.c
      >>
      >#include <stdio.h>
      >#define LEN 20
      >>
      >struct info {
      > char first[LEN];
      > char last[LEN];
      > int age;
      >};
      >>
      >struct pinfo {
      > char * first;
      > char * last;
      > int age;
      >};
      >>
      >int main(void)
      >{
      > struct info one = {"Opw", "Cde", 22};
      > struct pinfo two = {"Tydu", "Gqa", 33};
      > printf("%s %s is %d years old.\n", one.first, one.last, one.age);
      > printf("%s %s is %d years old.\n", two.first, two.last, two.age);
      > return 0;
      >}
      >>
      >//end test1.c
      >>
      >It can work.I know a little,but not very detail that the second struct pinfo is not good
      >in the above code. E.g, if the struct pinfo two is defined as an array
      >two[1] in the main function, it can not work. I just want to know the
      >reason in detail.
      >>
      It will work fine.
      >
      The answer to your question depends on your definition of "not good".
      The first form is OK if you want mutable strings with a known maximum
      length. The second is OK if you want constant strings and would be
      safer if changed to
      >
      struct pinfo {
      const char * first;
      const char * last;
      int age;
      };
      >
      >The better way to avoid this problem is malloc() and another pointer
      >point to the struct, and copy the strings, then free it. Is it right?
      >>
      Which problem are you trying to avoid?

      Comment

      • James Kuyper

        #4
        Re: Querry aoubt struct including character strings pointer

        bowlderyu wrote:
        Hello, all.
        If a struct contains a character strings, there are two methods to
        define the struct, one by character array, another by character pointer.
        E.g,
        //Program for struct includeing character strings, test1.c
        >
        #include <stdio.h>
        #define LEN 20
        >
        struct info {
        char first[LEN];
        char last[LEN];
        int age;
        };
        >
        struct pinfo {
        char * first;
        char * last;
        int age;
        };
        >
        int main(void)
        {
        struct info one = {"Opw", "Cde", 22};
        In this case, you are creating arrays whose contents can be safely
        modified, which are stored as part of 'one'.
        struct pinfo two = {"Tydu", "Gqa", 33};
        In this case, you are creating two unnamed arrays, whose contents cannot
        be safely modified. You are storing pointers to those arrays in 'two'.
        printf("%s %s is %d years old.\n", one.first, one.last, one.age);
        printf("%s %s is %d years old.\n", two.first, two.last, two.age);
        return 0;
        }
        >
        //end test1.c
        >
        It can work.I know a little,but not very detail that the second struct pinfo is not good
        There's no reason why it shouldn't be.
        in the above code. E.g, if the struct pinfo two is defined as an array
        two[1] in the main function, it can not work. I just want to know the
        reason in detail.
        If you change how you declare "two":

        struct pinfo two[1] = {"Tydu", "Gqa", 33};

        Then you also have to change how you use "two":

        printf("%s %s is %d years old.\n",
        two[0].first, two[0].last, two[0].age);

        Assuming you made matching changes, as above, there's no reason why it
        shouldn't work. Arrays of length 1 are generally pointless, but they're
        perfectly legal.
        The better way to avoid this problem is malloc() and another pointer
        point to the struct, and copy the strings, then free it. Is it right?
        Better in what sense? It's a lot more complicated than either method
        you've shown above. I'd use the first method if the strings need to be
        writable, but have a small fixed maximum length. I'd use the second
        method if the strings don't need to be writable, though I would change
        first and last to be "const char*" rather than "char*".

        The only time I would ever use the malloc() approach is if the length of
        the strings was not known at compile time, and has a large upper limit.

        Comment

        • Nick Keighley

          #5
          Re: Querry aoubt struct including character strings pointer

          please put your reply after the text you are replying to.
          Putting it before (as you did) is called "top posting".
          I have rearranged your post.

          On 21 Oct, 00:52, bowlderyu <bowlde...@gmai l.comwrote:
          Ian Collins <ian-n...@hotmail.co mwrites:
          bowlderyu wrote:
          If a struct contains a character strings, there are two methods to
          define the struct, one by character array, another by character pointer.
          E.g,
          //Program for struct includeing character strings, test1.c
          >
          #include <stdio.h>
          #define LEN 20
          >
          struct info {
          char first[LEN];
          char last[LEN];
          int age;
          };
          >
          struct pinfo {
          char * first;
          char * last;
          int age;
          };
          >
          int main(void)
          {
          struct info one = {"Opw", "Cde", 22};
          struct pinfo two = {"Tydu", "Gqa", 33};
          printf("%s %s is %d years old.\n", one.first, one.last, one.age);
          printf("%s %s is %d years old.\n", two.first, two.last, two.age);
          return 0;
          }
          >
          //end test1.c
          >
          It can work.I know a little,but not very detail that the second struct
          pinfo is not good
          in the above code. E.g, if the struct pinfo two is defined as an array
          two[1] in the main function, it can not work. I just want to know the
          reason in detail.
          >
          It will work fine.
          >
          The answer to your question depends on your definition of "not good".
          The first form is OK if you want mutable strings with a known maximum
          length. The second is OK if you want constant strings and would be
          safer if changed to
          >
          struct pinfo {
          const char * first;
          const char * last;
          int age;
          };
          >
          The better way to avoid this problem is malloc() and another pointer
          point to the struct, and copy the strings, then free it. Is it right?
          >
          Which problem are you trying to avoid
          >
          const char *, good sugesstion.
          >
          I don't know why it can not work for the case of two[1], so I want to
          avoid using char array. But now, it seems fine.
          >
          Would you please give more explanation about it?
          I mean for why not safe for non-constant strings.
          consider this:=

          #include <stdio.h>
          #define LEN 20

          struct info {
          char first[LEN];
          char last[LEN];
          int age;
          };

          struct pinfo {
          char * first;
          char * last;
          int age;
          };

          int main(void)
          {
          struct info one = {"Opw", "Cde", 22};
          struct pinfo two = {"Tydu", "Gqa", 33};

          /* this is ok */
          one.first[0] = 'X';

          /* this is NOT ok! */
          two.first[0] = 'X';

          return 0;
          }

          one contains modifiable arrays. So its ok to modify them.
          two points to string constants so it is *not* ok to modify
          them. You cannot modify string constants.

          This is ok:

          int main(void)
          {
          struct pinfo two;
          char first_arr[5] = "Tydu";

          two.first = first_arr;
          two.first[0] = 'X';

          return 0;
          }


          I do not understand what you mean when you say "I don't know why it
          can not work for the case of two[1], so I want to avoid using char
          array. But now, it seems fine.".

          Could you give an example which believe may be a problem.

          I am thinking about, seems clearly.

          --
          Nick Keighley

          1,3,7-trimethylxanthi ne -- a basic ingredient in quality software.

          Comment

          • Richard Heathfield

            #6
            Re: Querry aoubt struct including character strings pointer

            Nick Keighley said:

            <snip>
            You cannot modify string constants.
            s/cannot/must not/

            This, as Nick already knows, is one of those situations where the C
            language places a demand on the programmer rather than on the
            implementation. Complying with that demand is, therefore, a matter of
            self-discipline rather than implementation-imposed discipline. When
            programmers ignore such demands, very often they get away with it. But
            sometimes, they don't.

            It's almost like driving on the wrong side of the road. If you live in a
            relatively traffic-free area, you can actually drive on the wrong side of
            the road for quite extended periods - but you only have yourself to blame
            when you wrap yourself round a Mini Metro, with quite possibly fatal
            consequences.

            (It's not *quite* the same, because the police will enforce the law if they
            happen to spot you. If you live anywhere near me, the chances of that are
            pretty low, since the bobbies around here seem utterly incapable of
            distinguishing their seating arrangements from their arm joints.
            Nevertheless, the point stands.)

            <snip>

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • Joachim Schmitz

              #7
              Re: Querry aoubt struct including character strings pointer

              Richard Heathfield wrote:
              Nick Keighley said:
              >
              <snip>
              >
              >You cannot modify string constants.
              >
              s/cannot/must not/
              >
              This, as Nick already knows, is one of those situations where the C
              language places a demand on the programmer rather than on the
              implementation. Complying with that demand is, therefore, a matter of
              self-discipline rather than implementation-imposed discipline. When
              programmers ignore such demands, very often they get away with it. But
              sometimes, they don't.
              >
              It's almost like driving on the wrong side of the road. If you live
              in a relatively traffic-free area, you can actually drive on the
              wrong side of the road for quite extended periods - but you only have
              yourself to blame when you wrap yourself round a Mini Metro, with
              quite possibly fatal consequences.
              Happened to me once in the UK, driving on the right side (as opposed to
              left, not wrong, but to me that's the same thing 8-)), for several miles on
              an empty road in the very early hours of a day. Until a lorry approaching me
              on my side stopped me... fortunatly no fatalities, not even a crash.
              (It's not *quite* the same, because the police will enforce the law
              if they happen to spot you. If you live anywhere near me, the chances
              of that are pretty low, since the bobbies around here seem utterly
              incapable of distinguishing their seating arrangements from their arm
              joints. Nevertheless, the point stands.)
              Some pedestrians were staring at me, but indeed no bobbies in sight...

              Bye, Jojo


              Comment

              • CBFalconer

                #8
                OT wrong side driving (was: Querry aoubt struct including characterstring s pointer)

                Joachim Schmitz wrote:
                >
                .... snip ...
                >
                Happened to me once in the UK, driving on the right side (as
                opposed to left, not wrong, but to me that's the same thing 8-)),
                for several miles on an empty road in the very early hours of a
                day. Until a lorry approaching me on my side stopped me...
                fortunatly no fatalities, not even a crash.
                Way back when Sweden still drove on the left I visited there, and
                the UK. I deliberately didn't take up any opportunities to drive,
                because I expected habit would be over-riding. Even so, the London
                traffic made mighty efforts to get me, as I looked left, saw no
                traffic, and stepped out from the curb to be bashed from the right.

                In Germany, France, Italy, Austria I could take a different
                attitude. I did have problems with the German method of driving a
                DKW, with a pure toggle action on the throttle.

                --
                [mail]: Chuck F (cbfalconer at maineline dot net)
                [page]: <http://cbfalconer.home .att.net>
                Try the download section.

                Comment

                • Joachim Schmitz

                  #9
                  Re: OT wrong side driving (was: Querry aoubt struct including character strings pointer)

                  CBFalconer wrote:
                  Joachim Schmitz wrote:
                  >>
                  ... snip ...
                  >>
                  >Happened to me once in the UK, driving on the right side (as
                  >opposed to left, not wrong, but to me that's the same thing 8-)),
                  >for several miles on an empty road in the very early hours of a
                  >day. Until a lorry approaching me on my side stopped me...
                  >fortunatly no fatalities, not even a crash.
                  >
                  Way back when Sweden still drove on the left I visited there, and
                  the UK. I deliberately didn't take up any opportunities to drive,
                  because I expected habit would be over-riding. Even so, the London
                  traffic made mighty efforts to get me, as I looked left, saw no
                  traffic, and stepped out from the curb to be bashed from the right.
                  Been there, done that... on a dual carriageway (amongst others), thanks to a
                  great reaction of the driver I survived.
                  In Germany, France, Italy, Austria I could take a different
                  attitude. I did have problems with the German method of driving a
                  DKW, with a pure toggle action on the throttle.
                  Guess I'm to young for this, although I do recognize the brand name.

                  Bye, Jojo


                  Comment

                  • Moi

                    #10
                    Re: OT wrong side driving (was: Querry aoubt struct includingcharac ter strings pointer)

                    On Wed, 22 Oct 2008 09:06:49 +0200, Joachim Schmitz wrote:
                    CBFalconer wrote:
                    >In Germany, France, Italy, Austria I could take a different attitude.
                    >I did have problems with the German method of driving a DKW, with a
                    >pure toggle action on the throttle.
                    >
                    Guess I'm to young for this, although I do recognize the brand name.
                    >
                    Bye, Jojo
                    DKW used to be a brand of LKW.

                    But that was long before K&R.
                    :-)
                    AvK

                    Comment

                    • Joachim Schmitz

                      #11
                      Re: OT wrong side driving (was: Querry aoubt struct including character strings pointer)

                      Moi wrote:
                      On Wed, 22 Oct 2008 09:06:49 +0200, Joachim Schmitz wrote:
                      >
                      >CBFalconer wrote:
                      >
                      >>In Germany, France, Italy, Austria I could take a different
                      >>attitude. I did have problems with the German method of driving a
                      >>DKW, with a pure toggle action on the throttle.
                      >>
                      >Guess I'm to young for this, although I do recognize the brand name.
                      >>
                      >Bye, Jojo
                      >
                      DKW used to be a brand of LKW.
                      Not quite correct. DKW also build cars and motorbikes.
                      (for the benefit of non-German speakers: LKW == LastKraftWagen == Truck,
                      Lorry)

                      And yes I knew that.

                      I just don't get Chucks point about "toggle action on the throttle",
                      probably because I never drove one and only vaguely remember having seem
                      them (born in '61)
                      But that was long before K&R.
                      :-)
                      AvK
                      Bye, Jojo


                      Comment

                      • Dik T. Winter

                        #12
                        Re: OT wrong side driving (was: Querry aoubt struct including

                        In article <8352$48feee18$ 5350c294$24345@ cache110.multik abel.netMoi <root@invalid.a ddress.orgwrite s:
                        ....
                        DKW used to be a brand of LKW.
                        And of PKW. In the Netherlands it was frequently called "Duitse Kinder Wagen",
                        which German speakers would also be able to recognise.
                        --
                        dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
                        home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

                        Comment

                        • bowlderyu

                          #13
                          Re: Querry aoubt struct including character strings pointer

                          Nick Keighley <nick_keighley_ nospam@hotmail. comwrites:
                          please put your reply after the text you are replying to.
                          Putting it before (as you did) is called "top posting".
                          I have rearranged your post.
                          Thank you very much for your work.
                          And it's a very good suggestion.
                          >
                          On 21 Oct, 00:52, bowlderyu <bowlde...@gmai l.comwrote:
                          >Ian Collins <ian-n...@hotmail.co mwrites:
                          bowlderyu wrote:
                          >
                          >If a struct contains a character strings, there are two methods to
                          >define the struct, one by character array, another by character pointer.
                          >E.g,
                          >//Program for struct includeing character strings, test1.c
                          >>
                          >#include <stdio.h>
                          >#define LEN 20
                          >>
                          >struct info {
                          > char first[LEN];
                          > char last[LEN];
                          > int age;
                          >};
                          >>
                          >struct pinfo {
                          > char * first;
                          > char * last;
                          > int age;
                          >};
                          >>
                          >int main(void)
                          >{
                          > struct info one = {"Opw", "Cde", 22};
                          > struct pinfo two = {"Tydu", "Gqa", 33};
                          > printf("%s %s is %d years old.\n", one.first, one.last, one.age);
                          > printf("%s %s is %d years old.\n", two.first, two.last, two.age);
                          > return 0;
                          >}
                          >>
                          >//end test1.c
                          >>
                          >It can work.I know a little,but not very detail that the second struct
                          >pinfo is not good
                          >in the above code. E.g, if the struct pinfo two is defined as an array
                          >two[1] in the main function, it can not work. I just want to know the
                          >reason in detail.
                          >>
                          It will work fine.
                          >>
                          The answer to your question depends on your definition of "not good".
                          The first form is OK if you want mutable strings with a known maximum
                          length. The second is OK if you want constant strings and would be
                          safer if changed to
                          >>
                          struct pinfo {
                          const char * first;
                          const char * last;
                          int age;
                          };
                          Ok, the above words are help for me.
                          >>
                          >The better way to avoid this problem is malloc() and another pointer
                          >point to the struct, and copy the strings, then free it. Is it right?
                          >>
                          Which problem are you trying to avoid
                          >>
                          I made a mistake as to the struct pinfo two, so I want to avoid using
                          that struct. But now, for me, it is not a problem.
                          >const char *, good sugesstion.
                          >>
                          >I don't know why it can not work for the case of two[1], so I want to
                          >avoid using char array. But now, it seems fine.
                          >>
                          >Would you please give more explanation about it?
                          >I mean for why not safe for non-constant strings.
                          >
                          consider this:=
                          >
                          #include <stdio.h>
                          #define LEN 20
                          >
                          struct info {
                          char first[LEN];
                          char last[LEN];
                          int age;
                          };
                          >
                          struct pinfo {
                          char * first;
                          char * last;
                          int age;
                          };
                          >
                          int main(void)
                          {
                          struct info one = {"Opw", "Cde", 22};
                          struct pinfo two = {"Tydu", "Gqa", 33};
                          >
                          /* this is ok */
                          one.first[0] = 'X';
                          >
                          /* this is NOT ok! */
                          two.first[0] = 'X';
                          >
                          return 0;
                          }
                          >
                          one contains modifiable arrays. So its ok to modify them.
                          two points to string constants so it is *not* ok to modify
                          them. You cannot modify string constants.
                          >
                          This is ok:
                          >
                          int main(void)
                          {
                          struct pinfo two;
                          char first_arr[5] = "Tydu";
                          >
                          two.first = first_arr;
                          two.first[0] = 'X';
                          >
                          return 0;
                          }
                          >
                          It's a very simple but clear and helpful example.
                          >
                          I do not understand what you mean when you say "I don't know why it
                          can not work for the case of two[1], so I want to avoid using char
                          array. But now, it seems fine.".
                          >
                          Could you give an example which believe may be a problem.
                          >
                          >
                          >I am thinking about, seems clearly.
                          >
                          >
                          --
                          Nick Keighley
                          >
                          1,3,7-trimethylxanthi ne -- a basic ingredient in quality software.

                          Comment

                          • bowlderyu

                            #14
                            Re: Querry aoubt struct including character strings pointer

                            "Joachim Schmitz" <nospam.jojo@sc hmitz-digital.dewrite s:
                            Richard Heathfield wrote:
                            >Nick Keighley said:
                            >>
                            ><snip>
                            >>
                            >>You cannot modify string constants.
                            >>
                            >s/cannot/must not/
                            I see, now.
                            Thanks.
                            >>
                            >This, as Nick already knows, is one of those situations where the C
                            >language places a demand on the programmer rather than on the
                            >implementation . Complying with that demand is, therefore, a matter of
                            >self-discipline rather than implementation-imposed discipline. When
                            >programmers ignore such demands, very often they get away with it. But
                            >sometimes, they don't.
                            >>
                            >It's almost like driving on the wrong side of the road. If you live
                            >in a relatively traffic-free area, you can actually drive on the
                            >wrong side of the road for quite extended periods - but you only have
                            >yourself to blame when you wrap yourself round a Mini Metro, with
                            >quite possibly fatal consequences.
                            >
                            Happened to me once in the UK, driving on the right side (as opposed to
                            left, not wrong, but to me that's the same thing 8-)), for several miles on
                            an empty road in the very early hours of a day. Until a lorry approaching me
                            on my side stopped me... fortunatly no fatalities, not even a crash.
                            >
                            >(It's not *quite* the same, because the police will enforce the law
                            >if they happen to spot you. If you live anywhere near me, the chances
                            >of that are pretty low, since the bobbies around here seem utterly
                            >incapable of distinguishing their seating arrangements from their arm
                            >joints. Nevertheless, the point stands.)
                            >
                            Some pedestrians were staring at me, but indeed no bobbies in sight...
                            >
                            Bye, Jojo

                            Comment

                            • bowlderyu

                              #15
                              Re: Querry aoubt struct including character strings pointer

                              James Kuyper <jameskuyper@ve rizon.netwrites :
                              bowlderyu wrote:
                              >Hello, all.
                              >If a struct contains a character strings, there are two methods to
                              >define the struct, one by character array, another by character pointer.
                              >E.g,
                              >//Program for struct includeing character strings, test1.c
                              >>
                              >#include <stdio.h>
                              >#define LEN 20
                              >>
                              >struct info {
                              > char first[LEN];
                              > char last[LEN];
                              > int age;
                              >};
                              >>
                              >struct pinfo {
                              > char * first;
                              > char * last;
                              > int age;
                              >};
                              >>
                              >int main(void)
                              >{
                              > struct info one = {"Opw", "Cde", 22};
                              >
                              In this case, you are creating arrays whose contents can be safely
                              modified, which are stored as part of 'one'.
                              >
                              > struct pinfo two = {"Tydu", "Gqa", 33};
                              >
                              In this case, you are creating two unnamed arrays, whose contents
                              cannot be safely modified. You are storing pointers to those arrays in
                              two'.
                              >
                              > printf("%s %s is %d years old.\n", one.first, one.last, one.age);
                              > printf("%s %s is %d years old.\n", two.first, two.last, two.age);
                              > return 0;
                              >}
                              >>
                              >//end test1.c
                              >>
                              >It can work.I know a little,but not very detail that the second struct pinfo is not good
                              >
                              There's no reason why it shouldn't be.
                              >
                              >in the above code. E.g, if the struct pinfo two is defined as an array
                              >two[1] in the main function, it can not work. I just want to know the
                              >reason in detail.
                              >
                              If you change how you declare "two":
                              >
                              struct pinfo two[1] = {"Tydu", "Gqa", 33};
                              >
                              Then you also have to change how you use "two":
                              >
                              printf("%s %s is %d years old.\n",
                              two[0].first, two[0].last, two[0].age);
                              I regrat that I made a low-level and stupid mistake here,two[1]...
                              >
                              Assuming you made matching changes, as above, there's no reason why it
                              shouldn't work. Arrays of length 1 are generally pointless, but
                              they're perfectly legal.
                              >
                              >The better way to avoid this problem is malloc() and another pointer
                              >point to the struct, and copy the strings, then free it. Is it right?
                              >
                              Better in what sense? It's a lot more complicated than either method
                              you've shown above. I'd use the first method if the strings need to be
                              writable, but have a small fixed maximum length. I'd use the second
                              method if the strings don't need to be writable, though I would change
                              first and last to be "const char*" rather than "char*".
                              As I mentioned above, I found a method that used malloc() and free()
                              function for the struct pinfo two. As following

                              #include <stdio.h>
                              #include <stdlib.h>
                              #include <string.h>
                              #define LEN 20

                              struct pinfo {
                              char * first;
                              char * last;
                              int age;
                              };

                              void setinfo(struct pinfo * ps, char * first, char * last, int age);
                              void clean(struct pinfo * ps);
                              int main()
                              {
                              char fname[LEN] = "Wqc";
                              char lname[LEN] = "Oed";
                              int age = 33;
                              struct pinfo two;

                              setinfo(&two,fn ame,lname,age);
                              printf("%s %s is %d years old.\n", two.first, two.last, two.age);
                              //...other operations
                              clean(&two);
                              //...other operations
                              return 0;
                              }

                              void setinfo(struct pinfo * ps, char * first, char * last, int age)
                              {
                              ps->first = (char *) malloc (strlen(first) + 1);
                              strcpy(ps->first, first);
                              ps->last = (char *) malloc (strlen(last) + 1);
                              strcpy(ps->last, last);
                              ps->age = age;
                              }

                              void clean(struct pinfo * ps)
                              {
                              free (ps->first);
                              free (ps->last);
                              }

                              But as you mentioned below, it may be not a better method, even it works
                              well and seems clearly.

                              >
                              The only time I would ever use the malloc() approach is if the length
                              of the strings was not known at compile time, and has a large upper
                              limit.

                              Comment

                              Working...