How to set unlimited size of array of characters??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crystal2005
    New Member
    • Apr 2007
    • 44

    How to set unlimited size of array of characters??

    Hi everyone,

    I gonna ask how to set unlimited size of array of characters. According to the tutorial that i have found after amount of time of googling, array size should be defined when we declare it. In my task requirement, i'm gonna need 2D array to store the value of characters.

    E.g.

    char inputText[ ][20]; //20 characters is the maximum for each line

    In my case, i will put the user into an unlimited input of characters until certain rules are achieved. The problem that i encountered was, the above fragment of code always gives compilation error, it is said, the size of the array needs parameter whereas i do not want to define it.

    if i change the code to

    char inputText[ ][20]={ };

    it gives Segmentation fault (core dumped) after i input the character. Any help would be appreciated. Thank you ^_^
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    C does not give you the option of specifying the array size at run-time. You may want to use a linked list of dynamically-allocated line structures (link pointers + array of 20 chars). Dynamic allocation is accomplished by malloc.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by donbock
      C does not give you the option of specifying the array size at run-time.
      Yes it does. You create the array using malloc.

      Read this: http://bytes.com/topic/c/insights/77...rrays-revealed

      and look at the last example. Just use malloc instead of the C++ new operator.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by weaknessforcats
        Yes it does. You create the array using malloc.
        That is a question of symantics. Personally I would not call using malloc "specifying the array size". It is allocating a chunk of memory and treating it as though it is an array.

        The difference being that a static analysis tool can verify that the bounds of an array are not violated where as it would have a lot more trouble doing this for a chunk of allocated memory being treated as an array.

        However I think we can all agree that in C if you do not know your array size at compile time malloc is going to be the only option.

        Comment

        • crystal2005
          New Member
          • Apr 2007
          • 44

          #5
          Originally posted by weaknessforcats
          Yes it does. You create the array using malloc.

          Read this: http://bytes.com/topic/c/insights/77...rrays-revealed

          and look at the last example. Just use malloc instead of the C++ new operator.

          Hmmmmm.... that quite useful. Thanks

          Is the following code correct if i would like to implement it in my case
          Code:
          	int value;
          	char* array = new char[value];
          	char (*ptr)[20] = new char[value][20];

          Comment

          • ashitpro
            Recognized Expert Contributor
            • Aug 2007
            • 542

            #6
            make it something like this:
            Code:
            char **inputText;
            inputText = (int **) malloc(value * sizeof(char));
            for(i=0;i<value;i++)
            {
            	inputText[i] = (int *)malloc(20 * sizeof(char));
            }

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by Banfa
              That is a question of symantics. Personally I would not call using malloc "specifying the array size". It is allocating a chunk of memory and treating it as though it is an array.

              The difference being that a static analysis tool can verify that the bounds of an array are not violated where as it would have a lot more trouble doing this for a chunk of allocated memory being treated as an array.

              In fact, malloc allocates an array. You specifiy the number of bytes in your array rather than the number of elements. Then you can typecast the void* returned by malloc into any type of array pointer you want so long as the math works out and you don't overrun your allocation.

              As to stack arrays, I would not use them since you have no control over when the array is deleted. Passing the address of a local variable to a function is an accident waiting to happen. Better to use the heap so you can delete when you know it's appropriate.


              Originally posted by crystal2005
              int value;
              char* array = new char[value];
              char (*ptr)[20] = new char[value][20];
              In this case new char[value] returns the address of element 0 (which is a char) and assgn that address to array whrih requires the address of a char is correct.

              In the second case, again, the new operator returns the address of a array of 20 char. value says how many 20 char arrays to allocate. Assign the addres returned by new to a pointer to an array of 20 char is correct.


              Originally posted by ashitpro
              inputText = (int **) malloc(value * sizeof(char));
              is not correct. You need to malloc an array of char* rather than char.

              The correct code is:

              Code:
              inputText = (int **) malloc(value * sizeof(char*));

              Comment

              • ashitpro
                Recognized Expert Contributor
                • Aug 2007
                • 542

                #8
                Originally posted by weaknessforcats

                is not correct. You need to malloc an array of char* rather than char.

                The correct code is:

                Code:
                inputText = (int **) malloc(value * sizeof(char*));
                Oops,
                Apologies for the mistake

                Comment

                Working...