Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- dynamic_memory_allocation_for strings.c
- The C function fgets () is deprecated and discontinued in 2011.
- gets() is risky to use!
- https://www.geeksforgeeks.org/gets-is-risky-to-use/
- Thanks for the suggestions to
- Jark Clim
- https://www.facebook.com/metallicsoul92
- and
- Alan Sampson
- https://www.facebook.com/alansamps
- I did a program update and used the fgets() function
- instead of the gets() function.
- char *gets(char *str)
- https://www.tutorialspoint.com/c_standard_library/c_function_gets.htm
- char *fgets(char *str, int n, FILE *stream)
- https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm
- Before each call of function fgets()
- we must reserve memory for the string str[i].
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- https://www.facebook.com/dmilicev
- */
- #include <stdio.h>
- #include <stdlib.h> // for malloc(), calloc(), realloc(), free()
- #include <string.h> // for string functions
- #define LEN 255
- int main(void)
- {
- int i;
- char *str[3]; // here is 3 pointers to strings
- for(i=0;i<3;i++)
- {
- // reserve memory space (length LEN characters) to make room for str[i]
- str[i] = malloc( LEN * sizeof(char) );
- // Checking whether memory reallocation succeeded
- if( str[i] == NULL )
- {
- fprintf(stderr, "\n\n malloc() for str[%d] error! \n\n", i);
- exit(EXIT_FAILURE);
- }
- printf("\n Enter a string: ");
- fgets( str[i], LEN, stdin );
- printf("\n You entered: %s \n", str[i]);
- }
- return(0);
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment