473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 22868
donbock
2,426 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,065 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

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

Similar topics

43
by: Vladimir | last post by:
Method UnicodeEncoding.GetMaxByteCount(charCount) returns charCount * 2. Method UTF8Encoding.GetMaxByteCount(charCount) returns charCount * 4. But why that? Look: /* Each Unicode character...
6
by: cxcodexc | last post by:
I am trying to overload the +, *, and - operators using variables of type int but allowing them to hold unlimited size. Can anyone help me do this? thanks
2
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
5
by: =?Utf-8?B?ZHZhcm1h?= | last post by:
Hi I am executing a Sql Server SP from my C# app One of the parameters is of type varbinary. Since the content length of this parameter is variable I cannot set it to a pre-defined length. What...
1
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define...
1
by: peachdot | last post by:
hi, how do i create an unlimited array size?
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.