adding array into array of strings in c

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chirayu143
    New Member
    • Aug 2010
    • 6

    #1

    adding array into array of strings in c

    adding array into array of strings in c.....
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Who will understand your one liner?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You can't add to an arrray in C. Arrays have a fixed size.

      To add to an array of strings you need to create a new array that's bigger, copy the old array to it and then add the address of your added string.

      Ugly.

      Consider using a linked list.

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        Did you mean two Dimensional array?

        Comment

        • chirayu143
          New Member
          • Aug 2010
          • 6

          #5
          yes,..........2 dimensional array

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            If you use a 2D array for strings:

            Code:
            char array[100][20];
            then you are are limited to 100 strings and each string must be 19 characters or less.

            That is, there are now two limits in your code.

            Try to code so that your program does not have built-in limits.

            Note also that you can't add a string to this array. All you can do is copy a string to this array and overwrite the one that's already in there. Plus, if the string copied is more than 19 characters, you will corrupt memory and crash your program.

            Adding a string requires and array:

            Code:
            char array[101][20];
            But now you have to copy the first array to this one and then copy the added string to this one. And you haven't added a string to an array. All you have done is create a larger array and populated it.

            It boils down to the fact that you can't add elements to arrays. Therefore, you use a linked list instead.

            Comment

            • chirayu143
              New Member
              • Aug 2010
              • 6

              #7
              thanks for reply..it helped me..

              Comment

              Working...