By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

How do you make a large array of strings?

P: n/a
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
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
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
Expert 100+
P: 983
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

Post your reply

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