943,692 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3715
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 1st, 2009
1

char** for string array problem

Expand Post »
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...
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
pdrino is offline Offline
10 posts
since Dec 2007
Apr 1st, 2009
1

Re: char** for string array problem

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 1st, 2009
0

Re: char** for string array problem

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Apr 1st, 2009
0

Re: char** for string array problem

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Apr 1st, 2009
0

Re: char** for string array problem

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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Apr 1st, 2009
0

Re: char** for string array problem

@Narue:
Quote ...
>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:

Quote ...
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:

Quote ...
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:
Quote ...
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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
pdrino is offline Offline
10 posts
since Dec 2007
Apr 2nd, 2009
0

Re: char** for string array problem

nm
Last edited by jephthah; Apr 2nd, 2009 at 2:38 am.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Apr 2nd, 2009
0

Re: char** for string array problem

Click to Expand / Collapse  Quote originally posted by jephthah ...
nm
nm?
Reputation Points: 11
Solved Threads: 0
Newbie Poster
pdrino is offline Offline
10 posts
since Dec 2007
Apr 2nd, 2009
2

Re: char** for string array problem

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 2nd, 2009
0

Re: char** for string array problem

In fact it is 2nd case.

Quote ...
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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
pdrino is offline Offline
10 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: help needed
Next Thread in C Forum Timeline: kmeans program.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC