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!