Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define N 20
- char *get_string(char *str) // the function what allocate memory for the same string, and copy the string section in to the new string and print it.
- {
- int length;
- char *new_str;
- if (!str) // return a null pointer (0) if there is no available memory or if the arena has been detectably corrupted by storing outside the bounds of a block
- {
- printf("Error!Not enough memory!\n");
- exit(1);
- }
- else
- {
- length = strlen(str);
- new_str = (char*)malloc(sizeof(char)*(length + 1)); // Initial memory allocation, memory allocated using malloc
- strcpy(new_str, str);
- return new_str; // The return value of the function.
- }
- }
- void main()
- {
- char str[N], *new_str;
- printf("Please enter a string: ");
- scanf("%s", str);
- new_str = get_string(str);
- printf("The new string from the function is: %s\n", new_str); // printing a value of the new function
- free(new_str); // done with the int objects, and free the associated pointer
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement