/* This program tries to demonstrate two different types of dynamic * memory allocation using strings. */ #include #include int main( void ) { char * s1; int i; /* Method 1 */ s1 = (char *) malloc(sizeof(char)*4); if ( s1 == NULL ) { fprintf(stderr,"Error! Could not allocate space\n"); exit(-2); } /* if */ strcpy(s1, "abc"); { /* Within a new block statement, a local variable can be declared. */ char s2[4] = "abc"; if ( strcmp(s1, s2) == 0 ) { printf("s1 = %s equals s2 = %s\n", s1, s2); } else { printf("s1 = %s does not equal s2 = %s\n", s1, s2); } /* if */ } /* block */ free( s1 ); }