Dynamically allocate size of array for strings

This is a discussion on Dynamically allocate size of array for strings within the C Programming forums, part of the General Programming Boards category; Hello, guys! :-) How to dynamically allocate size of *str? This programm works fine, but in file can be more ...

  1. #1
    Unregistered
    Guest

    Question Dynamically allocate size of array for strings

    Hello, guys! :-)

    How to dynamically allocate size of *str?

    This programm works fine, but in file can be more lines than i can provide, and i want to read their all! :-)

    Or better write
    #define MAXSTRINGS 5000
    and don't **** my head? :-)

    code:
    -------------------------------
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define MAXSTRLEN 4096
    #define MAXSTRINGS 50

    int main(void)
    {
    FILE *fp;
    char *str[MAXSTRINGS]; /* ??? */
    char buf[MAXSTRLEN];
    int i;

    fp = fopen(PATH, "r");
    if (fp == NULL)
    exit(1);

    for(i=0; fgets(buf, MAXSTRLEN, fp) != NULL; i++) {
    str[i] = strdup(buf);
    if(str[i] == NULL)
    exit(1);
    }
    str[i] = NULL;

    fclose(fp);

    for(i=0; str[i]; i++)
    printf ("%s", str[i]);
    return 0;
    }
    -------------------------------
    THANKS!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use a linked list then. Or use realloc and use a char ** for your array.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One option would be read the file twice. The first time count the number of lines in the file. Then use malloc() to allocate that many strings. Then read the file again.

  4. #4
    Unregistered
    Guest
    OK, Thanks guys! :-)

    Considering aforesaid i have written this code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXSTRLEN	4096
    #define SOMEFILE	"somefile.txt"
    
    typedef struct list list;
    struct list
    {
    	char *str;
    	list *next;
    };
    
    list *item_new(char *str)
    {
    	list *lp;
    
    	if (str == NULL)
    		return NULL;
    	
    	lp = (list *) malloc(sizeof(list));
    	if (lp == NULL)
    		return NULL;
    	lp->str  = str;
    	lp->next = NULL;
    	
    	return lp;
    }
    
    list *item_add(list *lp, list *np)
    {
    	np->next = lp;
    	return np;
    }
    
    void free_list(list *lp)
    {
    	list *next;
    
    	for ( ; lp != NULL; lp = next) {
    		next = lp->next;
    		free(lp->str);
    		free(lp);
    	}
    }
    
    int main(void)
    {
    	FILE *fp;
    	list *lp;
    	char *tp, buf[MAXSTRLEN];
    
    	lp = NULL;
    	fp = fopen(SOMEFILE, "r");
    	if (fp == NULL)
    		exit(1);
    	while (fgets(buf, MAXSTRLEN, fp) != NULL) {
    		tp = strdup(buf);
    		if (tp == NULL)
    			exit(1);
    		lp = item_add(lp, item_new(tp));
    	}		
    	fclose(fp);
    
    	for ( ; lp != NULL; lp = lp->next)
    		printf ("%s", lp->str);
    
    	free_list(lp);
    
    	return 0;
    }
    Works fine, but i not certain that has done all correct, could not you correct me if i have allowed here blunders?
    Thank you!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Seems pretty good

    The only thing I can see is item_add doesn't guard against NULL pointers.

    Two minor points
    1. the (char*) cast on the malloc return is not necessary in ANSI-C. If you include stdlib.h there is no problem. However, if you fail to include stdlib.h, the cast suppresses an important warning.

    2. strdup is not an ANSI-C function. If you've got it, use it - but bear in mind that some people will not have that function.

  6. #6
    Unregistered
    Guest
    Thank you, Salem. :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 09:07 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21