Initialize char array in one line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashokd001
    New Member
    • Aug 2008
    • 30

    Initialize char array in one line

    Hi
    How do i reinitialize a char array in one line if i want to use the same array variable every time.
    Ex-

    char name[1024];

    -Ashok
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You can't 're-initialize' anything, you can just assign another value to the members
    of the array. Why do you want to do that on one line? Homework?

    kind regards,

    Jos

    Comment

    • huzen88
      New Member
      • Aug 2008
      • 3

      #3
      i'm not sure..
      i will ask my lecturer tomorrow.
      i hope i can help you..

      Comment

      • ashokd001
        New Member
        • Aug 2008
        • 30

        #4
        This is not just a home wark.
        If i want to use the same array , then i have to re-initialize it so it will be very easy if i can do it in one line.

        any suggestion..
        -ASK

        Comment

        • leejwen
          New Member
          • Jun 2007
          • 50

          #5
          Originally posted by ashokd001
          This is not just a home wark.
          If i want to use the same array , then i have to re-initialize it so it will be very easy if i can do it in one line.

          any suggestion..
          -ASK
          memset(name, 0, 1024);

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            You could assign new data to the array using strcpy or memcpy

            Comment

            • ashokd001
              New Member
              • Aug 2008
              • 30

              #7
              Can memset be use for C++.

              Regards,
              Ask

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Yes, of course if you are using C++ you should consider using a vector instead of an array with all its associated member.

                Comment

                • boxfish
                  Recognized Expert Contributor
                  • Mar 2008
                  • 469

                  #9
                  I've never seen memset before, so it seems wierd to me. Isn't strcpy a better choice somehow? It was designed for this, right?
                  Last edited by boxfish; Aug 23 '08, 04:00 PM. Reason: Needed extra comma.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by boxfish
                    I've never seen memset before, so it seems wierd to me. Isn't strcpy a better choice somehow? It was designed for this, right?
                    The strcpy() function needs a \0 terminated sequence of chars; memset() simply
                    sets every element to the same value. Also memcpy() can be of use; it depends.

                    kind regards,

                    Jos

                    Comment

                    Working...