You need to use "pointers" and the function "malloc" for doing this.
Here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _maxnumber_ 50
void main()
{
char *string[_maxnumber_]; // array of pointers
int number = 0;
int i = 0;
printf("Enter the number of string that you need to memorise: "
scanf("%d",&number);
if( number > _maxnumber_ )
{
printf("This number is too big ! "
exit(0);
}
getchar();
for( i = 0; i < number; i++ )
{
printf( "string %d: ", i + 1 );
if( ( string = ( char* )malloc(100)) == NULL ) // allocate memory dynamically to "string"
{
printf("can't allocate memory. "
break;
}
gets( string );
}
printf("\nYou have entered the following strings:\n"
for( i = 0; i < number; i++ )
{
puts( string );
free( string ); // free the memory
}
}
no mather the size of the strings that you have as input,if it is less or equal to "100 bytes" for each string this procedure should work perfectly.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.