-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
142 lines (108 loc) · 3.58 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Project 54 - More advanced topics about Pointer & Array of Strings
Description:
We'll understand the basics behind pointers and how to dereference
a pointer and we also learned a little bit more about arrays :)
Please, refer to the below comments :)
******************************************************************
Output:
6487552
Star Wars Episode V:
The string modified bt strcpy(): String copied to movie1
1
Star Wars: Episode VII
Star Wars: Episode VII - The Force Awakens
******************************************************************
Inspiration & Credits to:
thenewboston
C Programming Tutorial - 45 - Strings and Pointers
https://youtu.be/-9fqo94G6YU
&
mycodeschool
Character arrays and pointers - part 1
https://youtu.be/Bf8a6IC1dE8
Edited by J3
Date: Jul, 19/2020
*/
#include <stdio.h>
#include <string.h>
int main()
{
char movie1[] = "Star Wars Episode V:";
/*
Movie1 was essentially a special type of variable;
in other words it acted like a pointer.
it's not exactly a variable; it's actually a constant!
See its address:
*/
printf("%d\n", &movie1);
/* A constant we can't change it!
// Like this:
movie1 = "Star Wars Episode V: The Empire";
// Or like this:
movie1[21] = "The Empire";
puts(movie1);
Those commands above will not work:/
*/
/*
This pointer Movie1 is basically going
to point to the first elements address;
We're trying to change the pointer;
The pointer is actually a constant and
we can't change those so even if we
added those brackets afterward and try
to change your movie1...
this again... uh-uh not happening :/
We need to use a special function like
strcpy function (String Copy) or
what access each element individually one
by one as below:
*/
char newstrig[] = "String copied to movie1";
// strcpy(destination, source)
strcpy(movie1, newstrig);
printf("The string modified by strcpy(): %s\n", movie1);
printf("%c\n", movie1[22]); // This will print '1', the last char of movie1
/*
However it's kind of a tedious way to work with strings.
*/
/*
Would be awesome if we could just work
with these strings like a normal variable?
Check it out!
*/
char * movie2 = "Star Wars: Episode VII";
/*
Now Movie2 is not a constant!
What we did is we made this a pointer variable;
so whenever something is a variable we can indeed change it;
in other words we can print this out or put on
the left-hand side of the equal sign.
And it works ;)
*/
puts(movie2);
movie2 = "Star Wars: Episode VII - The Force Awakens";
puts(movie2);
/*
So whenever you make a pointer to a string
this pointer is a variable
so you can easily put it on the left hand side.
*/
//system("pause");
return 0;
/*
So the key concept takeaway from this
code is whenever you just make a
very simple array of characters it's
hard to change because that name of the
array is a constant and we can't easily
change constants;
However whenever we make a pointer to
a string, this pointer is a variable,
so you can easily put it on the left hand
side since all this is doing is its storing
the address of something so then we can
treat it like a string :)
thenewboston, Thanks you!!!!!!!!!!!!!!!!!!
*/
}