Solved

Pointer to list of strings

Posted on 2008-09-29
10
247 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
This collection of functions covers all the normal rounding methods of just about any numeric value.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

809 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