Skip to content

Commit c24557e

Browse files
committed
Use a NULL terminated list for function application
1 parent 4d399c5 commit c24557e

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

lessons/C Bootcamp.ipynb

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:21c3a341130308a0b513463b356fb1670395ed920b4496a9c57b5bcaa6eef31a"
4+
"signature": "sha256:cc56ddbccd6a0efcd809ff663d85c162d72633573ee08e4275cad5d8205d0380"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -772,13 +772,13 @@
772772
"source": [
773773
"### Pointers (continued)\n",
774774
"\n",
775-
"A common C idiom is `*q++ = *p++` to copy from a src array to a destination array, where p and q are both pointers. In English, this says\n",
775+
"**Differnt kinds of nothing**: There is a special null pointer indicated by the keyword NULL that points to nothing. It is typically used for pointer comparisons, since NULL pointers are guaranteed to compare as not equal to any other pointer (including another NULL). In paticular, it is often used as a sentinel value to mark the end of a list. In contrast a void pointer (void \\*) points to a memory location whose type is not decalred. It is used in C for generic operations - for example, `malloc` returns a void pointer. To totally confuse the beginning C student, there is also the NUL keyword, which refers to the `'\\0'` character used to terminate C strings. NUL and NULL are totally differnet beasts.\n",
776+
"\n",
777+
"**Deciphering pointer idioms**: A common C idiom that you should get used to is `*q++ = *p++` where p and q are both pointers. In English, this says\n",
776778
"\n",
777779
"* \\*q = \\*p (copy the variable pointed to by p into the variable pointed to by q)\n",
778780
"* increment q\n",
779-
"* increment p\n",
780-
"\n",
781-
"I suggest that you avoid such code."
781+
"* increment p"
782782
]
783783
},
784784
{
@@ -949,10 +949,22 @@
949949
"int main()\n",
950950
"{\n",
951951
" double a = 3;\n",
952-
" func fs[] = {square, cube};\n",
952+
" func fs[] = {square, cube, NULL};\n",
953953
" for (int i=0; i<2; i++) {\n",
954954
" printf(\"%.1f\\n\", apply(fs[i], a));\n",
955955
" }\n",
956+
"\n",
957+
" func *f = fs;\n",
958+
" printf(\"%.1f\\n\", apply(*f, a));\n",
959+
"\n",
960+
" f++;\n",
961+
" printf(\"%.1f\\n\", apply(*f, a));\n",
962+
"\n",
963+
" for (func *f=fs; f != NULL; f++) {\n",
964+
" printf(\"%.1f\\n\", apply(*f, a));\n",
965+
" }\n",
966+
" \n",
967+
"*/\n",
956968
"}"
957969
],
958970
"language": "python",
@@ -966,7 +978,7 @@
966978
]
967979
}
968980
],
969-
"prompt_number": 107
981+
"prompt_number": 118
970982
},
971983
{
972984
"cell_type": "code",
@@ -984,12 +996,14 @@
984996
"output_type": "stream",
985997
"stream": "stdout",
986998
"text": [
999+
"9.0\n",
1000+
"27.0\n",
9871001
"9.0\n",
9881002
"27.0\n"
9891003
]
9901004
}
9911005
],
992-
"prompt_number": 108
1006+
"prompt_number": 119
9931007
},
9941008
{
9951009
"cell_type": "code",

lessons/square2.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ double cube(double x)
2323
int main()
2424
{
2525
double a = 3;
26-
func fs[] = {square, cube};
27-
for (int i=0; i<2; i++) {
28-
printf("%.1f\n", apply(fs[i], a));
29-
}
26+
func fs[] = {square, cube, NULL};
27+
28+
for (func *f=fs; *f; f++) {
29+
printf("%.1f\n", apply(*f, a));
30+
}
3031
}

0 commit comments

Comments
 (0)