Solved

Pointer to list of strings

Posted on 2008-09-29
10
254 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use GPO to map & set default printer for all computers within AD security group 1 68
Homework Help 5 109
Odds of picking games correctly 4 159
COPY from excel to notepad 3 77
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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