Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ################################## DOES NOT WORK ######################################################
- #
- # Output:
- # gcc -Wall -Wextra -std=c11 crap.c -o crap.o
- # crap.c: In function ‘main’:
- # crap.c:10:7: warning: ‘firstName’ is used uninitialized in this function [-Wuninitialized]
- # scanf("%s", firstName);
- # ^
- # crap.c:12:7: warning: ‘lastName’ is used uninitialized in this function [-Wuninitialized]
- # scanf("%s", lastName);
- # ^
- #######################################################################################################
- #include <stdio.h>
- int main(void)
- {
- char * firstName;
- char * lastName;
- printf("What is your first name?\n");
- scanf("%s", firstName);
- printf("What is your last name?\n");
- scanf("%s", lastName);
- printf("Your full name is %s %s\n", firstName, lastName);
- return(0);
- }
- ####################### WORKS #####################################
- #include <stdio.h>
- int main(void)
- {
- char firstName[20];
- char lastName[20];
- printf("What is your first name?\n");
- scanf("%s", firstName);
- printf("What is your last name?\n");
- scanf("%s", lastName);
- printf("Your full name is %s %s\n", firstName, lastName);
- return(0);
- }
- ###################### THIS ALSO WORKS ###############################
- #include <stdio.h>
- #include <stdlib.h>
- int main(void)
- {
- char * firstName;
- char * lastName;
- /*
- * Curretly, firstName and lastName do not have any space
- * reserved in memory. Therefore we cannot save any variables to
- * the location they point to.
- */
- // Allocate 20 bytes. Don't forget to free.
- firstName = (char *) malloc(20);
- lastName = (char *) malloc(20);
- printf("What is your first name?\n");
- scanf("%s", firstName);
- printf("What is your last name?\n");
- scanf("%s", lastName);
- printf("Your full name is %s %s\n", firstName, lastName);
- free(firstName);
- free(lastName);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement