-
Notifications
You must be signed in to change notification settings - Fork 0
/
53_ft_heap_string.c
81 lines (64 loc) · 1.92 KB
/
53_ft_heap_string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Consider the following program:
#include <stdio.h>
char *ft_stack_string(void)
{
char str[14] = "Just a string";
return (str);
}
int main(void)
{
char *stack_string;
stack_string = ft_stack_string();
printf("%s\n", stack_string);
return (0);
}
Exceptionally, you can compile this code without -Wall -Wextra -Werror flags, simply by using
gcc c_file.c
Can you see the problem in this program and its output?
You can try to compile it with -Wall -Wextra -Werror flags to have a tip about the problem
For this exercice, you will need to write a function ft_heap_string that will allow you to compile the following program:
#include <stdio.h>
int main(void)
{
char *heap_string;
heap_string = ft_heap_string();
printf("%s\n", heap_string);
return (0);
}
The function must return a string containing "Just a string"
The provided program, when compiled with your "ft_heap_function" must print "Just a string" followed by a newline on standard output
You cannot modify the main function!
You must submit your function along with the provided main function
Your code must be compilable with
gcc -Wall -Wextra -Werror ft_heap_string.c
Allowed functions: malloc
Bonus:
Remember this exercice you did? (02_ft_mushka.c)
There is something wrong with it too, isn't there?
Can you fix it?
*/
#include <stdio.h>
#include <stdlib.h>
char *ft_heap_string(void)
{
char str[14] = "Just a string";
char *p = (char *)malloc(sizeof(char)); // ask to give memory in the heap // same as : malloc(14*sizeof(char))
int i;
i = 0;
while (str[i] != '\0')
{
p[i] = str[i]; // will save alla the element of string array into 'p'(heap)
i++;
}
str[i] = '\0'; // str[14] == '\0'
p[i] = '\0';
return (p);
}
int main(void)
{
char *heap_string;
heap_string = ft_heap_string();
printf("%s\n", heap_string);
return (0);
}