Pointer to list of strings

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
Who is Participating?
fluidtronicConnect With a Mentor Author Commented:
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.

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

Note that for resizing the list you can use realloc if needed.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

fluidtronicAuthor Commented:
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
   ...... read a string from a file
ozoConnect With a Mentor Commented:
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?
>> // 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 ?
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 ?
fluidtronicAuthor Commented:
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
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 :

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]))

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.