|
From: Khalid El-K. <kha...@ho...> - 2003-03-31 14:10:42
|
More than useful, thank you fellow :-)
>Here I send you an example of
>a string array resizing on-the-fly as you require...
>This possible solution is built with the
>"realloc( )" function as you indicated in
>your message.
>
>I hope this example illuminates your way toward
>a peaceful programming...Salam alecum!!.
>Ricardo.
>
>
>*******************PROGRAM*******************
>/* ALLOCATING AND REALLOCATING MEMORY */
>#include <stdio.h>
>#include <stdlib.h>
>
>int main()
>{
> char **p;
> int N,i;
>
> printf("Give me the initial array string dimension: ");
> scanf("%d", &N);
> fflush(stdin);
>
> /* As you want your initial array dimension is given
> by the user and it is allocated in this way */
>
> p= (char **)realloc(NULL,N*sizeof(char*));
> for(i=0; i<N ; i++)
> {
> /*strings of 10 char */
> p[i]= (char*)realloc(NULL,10*sizeof(char));
> printf("dame string %d: ",i);
> gets(p[i]);
> puts(p[i]);
> }
>
> /* if you need to store still more array strings
> you can reuse the reallocate function as follows */
>
> printf("Give me the new array string dimension: ");
> scanf("%d",&N);
> fflush(stdin);
>
> p= (char **)realloc(p,N*sizeof(char*));
> for(i=i; i<N ; i++)
> {
> p[i]= (char*)realloc(NULL,10*sizeof(char));
> printf("dame string %d: ",i);
> gets(p[i]);
> puts(p[i]);
> }
>
> /* Verification of correct string storing */
> printf("\n\n");
> for(i=0; i<N ; i++) puts(p[i]);
>
> /* At the end it is convenient to free the memory */
> for(i=0; i<N ; i++) free(p[i]);
> free(p);
>
> system("PAUSE");
> return 0;
>}
>
>*****************************
>
_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=features/junkmail
|