Solved

Pointer to list of strings

Posted on 2008-09-29
10
238 Views
Last Modified: 2010-04-15
Gentlemen
How can I declear a pointer to a list of strings when the number of strings are unknown.
Presently I decleare it as :
char *list[MAX_LIST];
At run time I allocate memory as pr. requirment :  list[i]=malloc(.....)

I would like to declear it independent of the number of MAX_LIST
Regards Per
0
Comment
Question by:fluidtronic
10 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22595227
Just do something like :
char **list;
 
/* when you know how many you need : */
list = (char**) calloc(MAX_LIST, sizeof(char));
 
/* and then fill it as you already do : */
list[i] = (char*) calloc(...);
/* etc. */

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22595230
Note that for resizing the list you can use realloc if needed.
0
 

Author Comment

by:fluidtronic
ID: 22595363
Thank you
What I do now is as below, but it does not work.
Only the first entry is filled in.

char **list;
void MyFunction(void)
{
   int i,n; char text[MAX_TEXT];
   n=50; // As an example
   list=(char**)calloc(n,sizeof(char));
   for(i=0;i<n;i++)
  {
    list[i]=(char*)calloc(1,MAX_TEXT*sizeof(char));
   ...... read a string from a file
   strcpy(list[i],text);
 }
}
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
ID: 22595468
list=(char**)calloc(n,sizeof(char)) // should be list=(char**)calloc(n,sizeof(char*))

...... read a string from a file // are you filling in text for other than the first entry?  are you filling in more than MAX_TEXT?
0
 

Accepted Solution

by:
fluidtronic earned 0 total points
ID: 22595618
ozo
Now it does work.
I'm filling in "n" strings of max length MAX_TEXT
A test of the expresions :
sizeof(char) = 1
sizeof(char*)= 4
sizeof(double*)= 4
I've learnt something today. Thank you.


0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22595620
>> // should be list=(char**)calloc(n,sizeof(char*))

Yes, that was my mistake ... Sorry :)


>> but it does not work.

If ozo's fix doesn't solve the problem, can you explain how it doesn't work ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22595729
May I ask why you select your own response as the solution ? Apart from the typo that ozo corrected, I didanswer your question, or am I missing something ?
0
 

Author Comment

by:fluidtronic
ID: 22595855
infinty
I'm a beginner in this comunity. I tried to accept ozo's proposal as the solution.
If it appears that it was my comment that solved the problem I apologize.

I thank both of you for your contributions.
This comunity works and I will upgrade to a paying member.
Regards Per
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22595885
Just fyi, this is the way you requested to close this question :

"fluidtronic has requested that this question be closed by accepting fluidtronic's comment #22595618 (0 points) as the solution and ozo's comment #22595468 (50 points) as the assisted solution"

Note that you can just close the question yourself by clicking the appropriate button.

Here's the relevant FAQ entry :

        http://www.experts-exchange.com/help.jsp#hi366
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 22597729
any reason why you don't do the following:

char * msgs[] =
{
"message zero",
"message one",
"message two",
         .
         .
         .
"message xxxx"
};

In your code, you could use (sizeof(msgs)/sizeof(msgs[0])) to get the maximum number of messages in the table.
I usually create a macro called:
#define  ELEMENTSOF(x)    (sizeof(x)/sizeof(x[0]))


0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Microsoft Dynamics Roles question 4 99
Permutation and Combination 9 86
Arduino EDI - Programming language 3 94
Odds of picking games correctly 4 89
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Viewers will learn the different options available in the Backstage view in Excel 2013.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question