Δημοσιεύτηκε: 17 Φεβ 2012, 20:01
από migf1
Ο σωστός κι ολοκληρωμένος κώδικας για αυτό που που πήγες να κάνεις είναι ο εξής:

Κώδικας: Επιλογή όλων

#include<stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXINPUT 256+1
int main( void )
{
char input[ MAXINPUT ] = {'\0'};
char *s = NULL;
int maxlen = 0; /* maximum length of s */
int len = 0; /* current length of s */

do {
printf("Give the len: ");
fgets( input, MAXINPUT, stdin );
maxlen = atoi(input);
} while ( maxlen < 1 );

maxlen++; /* for the '\0' at the end */
s = calloc(maxlen, sizeof(char));
if ( !s ) {
fputs("*** error, out of memory!\n", stderr);
exit( EXIT_FAILURE );
}

printf("Give a string : ");
fgets(s, maxlen, stdin);

len = strlen(s);
if ( s[ len-1 ] == '\n' )
s[ len-1 ] = '\0';

printf("You gave the string: %s\n", s);

system("pause");
free( s );
exit( EXIT_SUCCESS);
}


Μπορείς να καταλάβεις τι κάνει η κάθε του γραμμή και γιατί;