//useMalloc.c //John T. Rine //October 23, 2011 #include #include struct Test { char *message1; char *message2; }; typedef struct Test test; int main(int argc, char **argv) { //malloc a struct test *testPTR; testPTR = (test *)malloc(sizeof(test)); testPTR->message1 = "Hello "; testPTR->message2 = "world!"; printf("Message1 = %s, message2 = %s\n", testPTR->message1, testPTR->message2); free(testPTR); //malloc a char array int i = 0; int ii = 0; char * inputString = "John T. Rine\0"; char * string = NULL; printf("input string contains %s\n", inputString); while(*(inputString + i) != '\0') i++; printf("Character count = %d\n", i); string = (char *)malloc(sizeof(char) * (i + 1)); for(ii = 0; ii <= (i + 1); ii++) *(string + ii) = *(inputString + ii); printf("Dynamically allocated array contains %s\n", string); free(string); return 0; }