Every C programmer should learn some assembly
By chys on March 13th, 2009I am more convinced of this now.
One of the most frequently asked questions in C is the difference between a pointer and an array. A newbie in C often finds it “mission impossible” to differentiate between the following four variable types:
char p1[][8] = { "Hello", "world" };
char *p2[8] = { "Hello", "world" };
char (*p3)[8] = p1;
char **p4 = p2;
And it really is difficult to explain it clearly in a few words. However, if one knows some assembly, one can check the assembly listing generated by an assemblera compiler and at least the difference between p1 and p2 should be straightforward:
p1:
.string "Hello"
.zero 2
.string "world"
.zero 2
.LC0:
.string "Hello"
.LC1:
.string "world"
p2:
.long .LC0
.long .LC1
p3:
.long p1
p4:
.long p2
(I prefer the AT&T-style assembly)
I feel so lucky that I had learned some assembly used in NES before starting C. So for me “pointer” has always been a very natural concept and surely different from an array. Many poor freshmen undergrads had to begin with C++ without any knowledge in assembly or C or even any other language – I would have been crazy had I been under such a situation.
Related posts:
