473,406 Members | 2,467 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,406 software developers and data experts.

How do you make a large array of strings?

for instance sth like follows..where array size is 500

Expand|Select|Wrap|Line Numbers
  1.  char RetVal[500][];    
  2.  int32 IndexX;
  3.  RetVal[0][]= "Start Value";
  4.  for(IndexX = 1; IndexX < array_size-1; IndexX++)
  5.  {
  6.    RetVal[IndexX][]="middle values";
  7.  }
  8.  RetVal[array_size-1][]= "End Value";
where only the first and last value entered in the array are of my interest.
This piece of code above is incorrect. How do I code such a thing such that it gets correctly compiled on a c-compiler?
Nov 1 '10 #1
2 2486
Banfa
9,065 Expert Mod 8TB
Assuming C89/C90

The problem with the code is that char RetVal[500][]; is not valid. You would have to put a value into the size of the last index.

In any array it is only valid to omit the size of the first index (the one you set to 500) and then only if you provide an initialiser so that the compiler can calculate the size.

You have 2 choices
  1. Put in a size in the second index making sure that it is long enough to hold any string you might assign to the array.
  2. Instead of an array of character arrays use an array of pointers, char* RetVal[500];. Then are you need to fill in each one allocate memory for it using malloc.


Of course if you are actually using C++ you should be using a vector of strings std::vector<std::string> >
Nov 1 '10 #2
Oralloy
988 Expert 512MB
NUR,

Are you trying to avoid dynamic memory?

The reason I ask is because Banfa is giving you the best suggestions.

Not only will line 1,
char RetVal[500][];
not compile, but line 3,
RetVal[0][]= "Start Value";
will also fail to compile, when used as is.

So, as Banfa observes, if you are using C++, the vector of strings method is really the best (and safest) way to go about managing your array.

However, as you are apparently using C
This piece of code above is incorrect. How do I code such a thing such that it gets correctly compiled on a c-compiler?
so you will have to do something similar to the following code:
Expand|Select|Wrap|Line Numbers
  1. #define array_size 500
  2. typedef char ManyStrings[array_size][500];
  3.  
  4. ManyStrings *myFunc()
  5. {
  6.   static ManyStrings RetVal;
  7.  
  8.   strcpy(RetVal[0], "Start Value");
  9.   for(IndexX = 1; IndexX < array_size-1; IndexX++)
  10.   {
  11.     strcpy(RetVal[IndexX], "middle values");
  12.   }
  13.  
  14.   strcpy(RetVal[array_size-1], "End Value");
  15.  
  16.   return &RetVal;
  17. }
However, my example is simply an extension of the code you provided. It really wastes memory, and not amenable to arrays of truly significant size. Specifically, the array, as declared, takes 250k of memory.

How you ultimately resolve your problem depends on the problem's requirements.
Nov 1 '10 #3

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

Similar topics

2
by: Mark | last post by:
I am using PHP 4.3.5. I am having a problem in which my script is crashing when it is using a large array. I have simplified the problem down to the following example: <? set_time_limit(0);...
8
by: Pjotr Wedersteers | last post by:
Hello, I tried to create an array with 1000 cells, keys 0 thru 999 using $myarr = array (1000); But this leads to an array of 1 cell with value 1000; Now I have a workable solution for...
5
by: apm | last post by:
Any and all: Is there an efficient way to pass a large array from .NET to COM? Can references (or pointer) be passed from COM to NET and NET to COM without the object it refers to being copied?...
6
by: Howard Weiss | last post by:
I am reading a 160 mB file in my application. I want to access the file as shorts. FileStream->Read wants to access the file as Byte so I do the following 1) Read the filesize 2) Create a Byte...
16
by: Claudio Grondi | last post by:
What started as a simple test if it is better to load uncompressed data directly from the harddisk or load compressed data and uncompress it (Windows XP SP 2, Pentium4 3.0 GHz system with 3 GByte...
4
by: MrBiggles | last post by:
Here's the sitch: I read in a csv file with 60000 lines (20 fields per record), and store the data to a local array. The file is read in and stored just fine and pretty quick. Now, if I try to...
3
by: johnmann56 | last post by:
Hello. I have a small C# program which uses a number of large arrays of double. The program runs fine on my desktop PC which has 2 GB of memory, but when run on my laptop with 512 MB the laptop...
10
by: Krustov | last post by:
$rambo="daffy duck"; $rambo="mad max"; $rambo="daffy duck"; $rambo="superman"; etc etc How do i remove duplicate strings from a array ? . ('daffy duck' could appear more than twice in the...
10
by: Adam Schaible | last post by:
Hello! My project has the following interface: (screenshot works the best) http://www.screencast.com/users/aschaible/folders/Jing/media/65d7db9d-2499-48b2-84e9-4a7af883e3c9 Device groups...
10
by: pram29 | last post by:
Hello, I want to have a large array. I have a structure which contains 6 fields, 4 of them are unsigned char and 2 of them are unsigned short. I should have an array of 80 elements of type the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.