Solved

Pointer to list of strings

Posted on 2008-09-29
10
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all theā€¦
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Viewers will learn the different options available in the Backstage view in Excel 2013.

707 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