469,327 Members | 1,219 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

How to set unlimited size of array of characters??

crystal2005
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 ^_^
Mar 25 '09 #1
7 20809
donbock
2,422 Expert 2GB
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.
Mar 25 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 25 '09 #3
Banfa
9,064 Expert Mod 8TB
@weaknessforcats
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.
Mar 26 '09 #4
@weaknessforcats

Hmmmmm.... that quite useful. Thanks

Is the following code correct if i would like to implement it in my case
Expand|Select|Wrap|Line Numbers
  1.     int value;
  2.     char* array = new char[value];
  3.     char (*ptr)[20] = new char[value][20];
  4.  
Mar 26 '09 #5
ashitpro
542 Expert 512MB
make it something like this:
Expand|Select|Wrap|Line Numbers
  1. char **inputText;
  2. inputText = (int **) malloc(value * sizeof(char));
  3. for(i=0;i<value;i++)
  4. {
  5.     inputText[i] = (int *)malloc(20 * sizeof(char));
  6. }
  7.  
Mar 26 '09 #6
weaknessforcats
9,208 Expert Mod 8TB
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.


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.


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

The correct code is:

Expand|Select|Wrap|Line Numbers
  1. inputText = (int **) malloc(value * sizeof(char*)); 
Mar 26 '09 #7
ashitpro
542 Expert 512MB
@weaknessforcats
Oops,
Apologies for the mistake
Mar 27 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

43 posts views Thread by Vladimir | last post: by
6 posts views Thread by cxcodexc | last post: by
5 posts views Thread by =?Utf-8?B?ZHZhcm1h?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.