



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




char ** read_names ( int * num_names ) ;
void free_names ( char ** names , int num_names ) { for ( int i = 0; i < num_names ; i ++) { free ( names [ i ]) ; } free ( names ) ; }
int main () { int num_names = 0; char ** names = read_names (& num_names ) ;
printf ("\ nYou entered % d name ( s ) :\ n " , num_names ) ; for ( int i = 0; i < num_names ; i ++) { printf ("% s \ n " , names [ i ]) ; }
free_names ( names , num_names ) ; return 0; }
1 # include < stdio .h > 2 # include < stdlib .h > 3 # include < string .h > 4 5 # define SPACE 100 6 # define SECRET_NUM_1 7 7 # define SECRET_NUM_2 8 8 9 int main () { 10 const char * msg1 = " Hello , World !\ n "; 11 const char * msg2 = " We love Computer Science !"; 12 13 char * str = ( char *) malloc ( SPACE ) ; 14 if ( str == NULL ) { 15 printf (" Memory allocation failed .\ n ") ; 16 return 1; 17 } 18 19 strncpy ( str , msg1 , SPACE ) ; 20 strncpy ( str + SECRET_NUM_1 , msg2 + SECRET_NUM_2 , SPACE / 2) ; 21 str [15] = ’\0 ’; 22 23 printf ("% s \ n " , str ) ; 24 25 free ( str ) ; 26 }