Solved

Pointer to list of strings

Posted on 2008-09-29
10
220 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now