Con un Malloc dovresti ugualmente conoscere la Dimensione...
L'unica e' crearti una funzione utilizzando un realloc...codice:#define DIM 30 ... char *string; string = (char *) malloc (DIM * sizeof(char));
Non ti assicuro niente per la funzione.. Dagli un occhiata tu... Sono stanco e non ho voglia di pensare...:tongue:codice:... char *string; char chr; unsigned int i; do { chr = getch(); string = (char *) realloc (string, (1+i)*sizeof(char)); // Devi fare i controlli if (string == NULL) string[i] = chr; i++; } while(chr != '\r' && chr != '\n'); ...
R e a l l o c
Descriptioncodice:#include <stdlib.h> void *realloc(void *block, size_t size);
Reallocates main memory.
realloc attempts to shrink or expand the previously allocated block to size bytes. If size is zero, the memory block is freed and NULL is returned. The block argument points to a memory block previously obtained by calling malloc, calloc, or realloc. If block is a NULL pointer, realloc works just like malloc.
realloc adjusts the size of the allocated block to size, copying the contents to a new location if necessary.
Return Value
realloc returns the address of the reallocated block, which can be different than the address of the original block.
If the block cannot be reallocated, realloc returns NULL.
If the value of size is 0, the memory block is freed and realloc returns NULL.M a l l o c
Descriptioncodice:#include <stdlib.h> or #include<malloc.h> void *malloc(size_t size);
malloc allocates a block of size bytes from the memory heap. It allows a program to allocate memory explicitly as it's needed, and in the exact amounts needed.
Allocates main memory.The heap is used for dynamic allocation of variable-sized blocks of memory. Many data structures, for example, trees and lists, naturally employ heap memory allocation.
For 16-bit programs, all the space between the end of the data segment and the top of the program stack is available for use in the small data models, except for a small margin immediately before the top of the stack. This margin is intended to allow the application some room to make the stack larger, in addition to a small amount needed by DOS.
In the large data models, all the space beyond the program stack to the end of available memory is available for the heap.
Return Value
On success, malloc returns a pointer to the newly allocated block of memory. If not enough space exists for the new block, it returns NULL. The contents of the block are left unchanged. If the argument size == 0, malloc returns NULL.