Different Size arrays when reading from a stream
When writing characters to an array from a stream, is there a way to make the length of the array the exact number of characters if the size of th开发者_JAVA技巧e stream is unknown at compile? For example when reading text input into an array and the size of text can be any length.
Find the size of the file
fseek(fp, 0, SEEK_END);
size = ftell(fp);
/* Rewind. */
fseek(fp, 0, SEEK_SET);
Allocate the memory
char *buf = malloc(size);
It has been a while since I've written C, but I believe you can use the malloc() function to dynamically allocate a block of memory.
char* str = malloc( sizeof( char ) * lengthOfStream ) ;
Are u asking about to increase the lenght of the each array element size or wants to increase the lenght or size of the array(no.of elements)
if u wants to increase the no.of elements then use this syntax
char array[];
if you are talking about the individual element its not possible to increase because the datatype by whixh we decleared an array was constant.
Thank you
精彩评论