char** for string array problem

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2007
Posts: 10
Reputation: pdrino is an unknown quantity at this point 
Solved Threads: 0
pdrino pdrino is offline Offline
Newbie Poster

char** for string array problem

 
1
  #1
Apr 1st, 2009
Hi, I use char* for string handling because I read strings from a file and I don't know the length of these strings. The problem comes when I need to keep some strings in an array. I think char** is the best thing for that goal. But again since I don't know how many strings I will hold, I'm stuck in allocating memory for new strings to be hold. For example;
  1. char* str;
  2. char** match_list;
  3. while (fscanf(inp, "%s", str) != EOF) {
  4. if (matches(str)) {
  5. *match_list = (char**)malloc(sizeof(char*));
  6. *(match_list + i) = str;
  7. i++;
  8. }
  9. }

matches is the function checks the string for my intention. The line that malloc is included is a bit silly, I know . I need to access the strings in future. I think I could explain my problem. I want to allocate a new char* space and bind my str to it. But how?
Thanks...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: char** for string array problem

 
1
  #2
Apr 1st, 2009
Let's start by clearing up an obvious misunderstanding about pointers.

>char *str;
>fscanf(inp, "%s", str);
This is broken. An uninitialized pointer does not mean a pointer to unlimited space. It means you're not allowed to dereference that pointer or compare it to anything until you initialize it.

You still need to guesstimate a size for reading strings from input, even if that size is a chunk and you append the chunks using dynamic memory. Get that right and then you can worry about an array of strings, because the solution is very similar.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: char** for string array problem

 
0
  #3
Apr 1st, 2009
Well I really couldn't get what you meant but I will answer to the point I think you asked...k

Well you can allocate two dimensional array as follows:
  1. char **array; //Your two dimensional array
  2. int str_nos=10; //No of strings you want to have
  3. int str_len=60; //Maximum length of each string
  4. array=malloc( str_nos * sizeof(char *) );
  5. //Above st allocates memory for no of char pointers or strings
  6. for(i=0;i<str_nos;i++)
  7. {
  8. a[i]=malloc( str_len * sizeof(char *) );
  9. }

In this way you can dynamically allocate the number of strings and also their lengths by just altering he str_nos and str_len .I hope this helps
Last edited by csurfer; Apr 1st, 2009 at 1:00 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: char** for string array problem

 
0
  #4
Apr 1st, 2009
Just a tip: since you are reallocating new memory when you run out of space, you probably want to use realloc. This will keep all the data you had copied into your array previously.
Last edited by death_oclock; Apr 1st, 2009 at 2:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: char** for string array problem

 
0
  #5
Apr 1st, 2009
instead of using malloc() (or realloc) poorly.... might i suggest sticking with the tried and true:
  1. char stringblock[MAX_NUM_LINES][MAX_LINE_LEN];
because i'm fairly certain you're not doing an embedded application that is limited for RAM.

but mangling malloc() like you're doing will certainly find the limits of your computer memory after you fill it full of leaks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: pdrino is an unknown quantity at this point 
Solved Threads: 0
pdrino pdrino is offline Offline
Newbie Poster

Re: char** for string array problem

 
0
  #6
Apr 1st, 2009
@Narue:
>char *str;
>fscanf(inp, "%s", str);
This is broken. An uninitialized pointer does not mean a pointer to unlimited space. It means you're not allowed to dereference that pointer or compare it to anything until you initialize it.
thanks for reply. although you said it is broken I initialize the str every time I read a word from the file. After initialziation I work with str and matches function work properly.

@csurfer:

In this way you can dynamically allocate the number of strings and also their lengths by just altering he str_nos and str_len .I hope this helps
Thanks for reply but in your way I think i have to read file twice to have a correct size of match list. Isn't that a problem for performance when reading big files?

@jephthah:

char stringblock[MAX_NUM_LINES][MAX_LINE_LEN];
Thanks for reply. I don't know what will be the values of MAX_NUM_LINES and MAX_LINE_LEN values before I read the file and changing these values is not possible after run time I think.

@death_oclock:
Just a tip: since you are reallocating new memory when you run out of space, you probably want to use realloc. This will keep all the data you had copied into your array previously.
Is that possible I allocate one char* space beforehand and reallaocate space one by one after I found new words that matches my specifications? Using that way make sense but I have a char* named str and I want to keep that pointer in an array named match_list. As I wrote on the code it is char** type. I don't know how to tie these values. Thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: char** for string array problem

 
0
  #7
Apr 2nd, 2009
nm
Last edited by jephthah; Apr 2nd, 2009 at 2:38 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: pdrino is an unknown quantity at this point 
Solved Threads: 0
pdrino pdrino is offline Offline
Newbie Poster

Re: char** for string array problem

 
0
  #8
Apr 2nd, 2009
Originally Posted by jephthah View Post
nm
nm?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: char** for string array problem

 
2
  #9
Apr 2nd, 2009
>although you said it is broken I initialize the str every time I read a word from the file.
Hmm, I can interpret this one of two ways:
  1. You're saying the code you posted isn't equivalent to the code you're actually using.
  2. You're saying that I'm wrong and the code you posted isn't broken.
If it's the first case then I would highly recommend that you post equivalent examples to your real code if you want any useful help. If it's the second case then I pity you, because I'm very rarely wrong when it comes to the standard language/library, and when I am it's not the people asking beginner questions who are able to correct me successfully.

Arbitrary length input takes more work, and often the basic input functions are weak without a bit of scaffolding around them to get what you want. For example, here's a delimited string input function that can be used to read words:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define GROW_BY 32 /* Arbitrary size */
  6.  
  7. char *reads ( FILE *in, char *delim )
  8. {
  9. char *result = NULL;
  10. size_t size = 0;
  11. size_t capacity = 0;
  12. int ch;
  13.  
  14. while ( ( ch = getc ( in ) ) != EOF ) {
  15. if ( size == capacity ) {
  16. char *temp;
  17.  
  18. capacity += GROW_BY;
  19. temp = realloc ( result, capacity + 1 );
  20.  
  21. if ( temp == NULL ) {
  22. /*
  23.   This is a weak solution because it results in
  24.   loss of data. Recommend better error handling
  25.   */
  26. free ( result );
  27. result = NULL;
  28. break;
  29. }
  30.  
  31. result = temp;
  32. }
  33.  
  34. /* Order is important here; run *after* the resize step */
  35. if ( strchr ( delim, ch ) != NULL )
  36. break;
  37.  
  38. result[size++] = ch;
  39. }
  40.  
  41. if ( result != NULL )
  42. result[size] = '\0';
  43.  
  44. return result;
  45. }
  46.  
  47. int main ( void )
  48. {
  49. char *s;
  50.  
  51. while ( ( s = reads ( stdin, " \n" ) ) != NULL ) {
  52. printf ( ">%s<\n", s );
  53. free ( s );
  54. }
  55.  
  56. return 0;
  57. }
Take careful note of how I handled the memory, this is a common pattern that you can reuse for an array of pointers to char. You can use this example to neatly fix your broken code and find the answer to the original problem of creating a dynamic array. You're welcome.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: pdrino is an unknown quantity at this point 
Solved Threads: 0
pdrino pdrino is offline Offline
Newbie Poster

Re: char** for string array problem

 
0
  #10
Apr 2nd, 2009
In fact it is 2nd case.

because I'm very rarely wrong when it comes to the standard language/library, and when I am it's not the people asking beginner questions who are able to correct me successfully.
Also if you have a big experience then you should know it is not a big deal that people can correct you. If you are uncomfortable with that using forums is not suitable for you. I'm not saying I am an expert but you are wrong about what you've written in previous post. Technically speaking, I initialize and reinitiaize str pointer with every word I read from file. It is not a char pointer array it is just a char pointer. After that I make comparison or whatever I need. At least I know that is possible and I suggest you to accept your fault. I don't want to be offending but if you want I could send you the code. Because it is one of my friend's project I prefer not to publish it here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1571 | Replies: 18
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC