|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:21c3a341130308a0b513463b356fb1670395ed920b4496a9c57b5bcaa6eef31a" |
| 4 | + "signature": "sha256:cc56ddbccd6a0efcd809ff663d85c162d72633573ee08e4275cad5d8205d0380" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
772 | 772 | "source": [ |
773 | 773 | "### Pointers (continued)\n", |
774 | 774 | "\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", |
776 | 778 | "\n", |
777 | 779 | "* \\*q = \\*p (copy the variable pointed to by p into the variable pointed to by q)\n", |
778 | 780 | "* increment q\n", |
779 | | - "* increment p\n", |
780 | | - "\n", |
781 | | - "I suggest that you avoid such code." |
| 781 | + "* increment p" |
782 | 782 | ] |
783 | 783 | }, |
784 | 784 | { |
|
949 | 949 | "int main()\n", |
950 | 950 | "{\n", |
951 | 951 | " double a = 3;\n", |
952 | | - " func fs[] = {square, cube};\n", |
| 952 | + " func fs[] = {square, cube, NULL};\n", |
953 | 953 | " for (int i=0; i<2; i++) {\n", |
954 | 954 | " printf(\"%.1f\\n\", apply(fs[i], a));\n", |
955 | 955 | " }\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", |
956 | 968 | "}" |
957 | 969 | ], |
958 | 970 | "language": "python", |
|
966 | 978 | ] |
967 | 979 | } |
968 | 980 | ], |
969 | | - "prompt_number": 107 |
| 981 | + "prompt_number": 118 |
970 | 982 | }, |
971 | 983 | { |
972 | 984 | "cell_type": "code", |
|
984 | 996 | "output_type": "stream", |
985 | 997 | "stream": "stdout", |
986 | 998 | "text": [ |
| 999 | + "9.0\n", |
| 1000 | + "27.0\n", |
987 | 1001 | "9.0\n", |
988 | 1002 | "27.0\n" |
989 | 1003 | ] |
990 | 1004 | } |
991 | 1005 | ], |
992 | | - "prompt_number": 108 |
| 1006 | + "prompt_number": 119 |
993 | 1007 | }, |
994 | 1008 | { |
995 | 1009 | "cell_type": "code", |
|
0 commit comments