Notes+10.17

Today we looked at 3 implementations of Sorting and some puzzles.

Get in the habit of making little tiny test programs to verify that you understand how things work. For each of these snippets of C code, predict what you'll get. (Answers below) code format="c" rsmoody@ctec:~/puzzles$ cat z1.c
 * 1) include 

main { printf("Hello, world.\n"); }

rsmoody@ctec:~/puzzles$ cat z2.c
 * 1) include 

struct Student { char *name; int SID; char *phone; float height; // cm. int birthday; // {0..366} int programPoints; int examPoints; int participationPoints; };

main { Student.name = "Ebineezer". printf("Student.name=%s\n", Student.name); }

rsmoody@ctec:~/puzzles$ cat z3.c
 * 1) include 

struct Student { char *name; int SID; char *phone; float height; // cm. int birthday; // {0..366} int programPoints; int examPoints; int participationPoints; };

struct Student stud1;

main { stud1.name = "Ebineezer". printf("stud1.name=%s\n", stud1.name); }

rsmoody@ctec:~/puzzles$ cat z4.c
 * 1) include 

struct Student { char *name; int SID; char *phone; float height; // cm. int birthday; // {0..366} int programPoints; int examPoints; int participationPoints; };

struct Student stud1;

main { stud1->name = "Ebineezer". printf(stud1->name=%s\n", stud1->name); }

rsmoody@ctec:~/puzzles$ cat z5.c
 * 1) include 

struct Student { char *name; int SID; char *phone; float height; // cm. int birthday; // {0..366} int programPoints; int examPoints; int participationPoints; };

struct Student stud1;

main { stud1.name = "Ebineezer"; printf("stud1.name=%s\n", stud1.name); }

rsmoody@ctec:~/puzzles$ cat z6.c
 * 1) include 

struct Student { char *name; int SID; char *phone; float height; // cm. int birthday; // {0..366} int programPoints; int examPoints; int participationPoints; };

struct Student stud1; struct Student *studPtr = &stud1;

main { studPtr->name = "Alexei"; printf("studPtr->name=%s\n", studPtr->name); printf("stud1.name=%s\n", stud1.name); printf("&stud1=%i\n", &stud1); printf("stud1=%i\n", stud1); printf("&studPtr=%i\n", &studPtr); printf("studPtr=%i\n", studPtr); }

rsmoody@ctec:~/puzzles$ cat z7.c
 * 1) include 

struct Student { char *name; int SID; char *phone; float height; // cm. int birthday; // {0..366} int programPoints; int examPoints; int participationPoints; };

struct Student stud1; struct Student *studPtr = &stud1;

main { studPtr->name = "Alexei"; printf("studPtr->name =%s\n", studPtr->name); printf("stud1.name =\t%s\n", stud1.name); printf("&stud1 =\t%i\n", &stud1); printf("stud1 =\t%i\n", stud1); printf("&studPtr =\t%i\n", &studPtr); printf("studPtr =\t%i\n", studPtr); }

code Results: code

rsmoody@ctec:~/puzzles$ c z1 ~/puzzles rsmoody@ctec:~/puzzles$ z1 Hello, world. rsmoody@ctec:~/puzzles$ c z2 ~/puzzles z2.c: In function âmainâ: z2.c:15: error: âStudentâ undeclared (first use in this function) z2.c:15: error: (Each undeclared identifier is reported only once z2.c:15: error: for each function it appears in.) z2.c:16: error: request for member âprintfâ in something not a structure or union rsmoody@ctec:~/puzzles$ c z3 ~/puzzles z3.c: In function âmainâ: z3.c:18: error: request for member âprintfâ in something not a structure or union rsmoody@ctec:~/puzzles$ c z4 ~/puzzles z4.c: In function âmainâ: z4.c:17: error: invalid type argument of â->â (have âstruct Studentâ) z4.c:18: error: request for member âprintfâ in something not a structure or union z4.c:18: error: invalid type argument of â->â (have âstruct Studentâ) z4.c:18: error: expected expression before â%â token z4.c:18: error: stray â\â in program z4.c:18:28: warning: missing terminating " character z4.c:18: error: missing terminating " character z4.c:19: error: expected â;â before â}â token rsmoody@ctec:~/puzzles$ c z5 ~/puzzles rsmoody@ctec:~/puzzles$ z5 stud1.name=Ebineezer rsmoody@ctec:~/puzzles$ c z6 ~/puzzles rsmoody@ctec:~/puzzles$ z6 studPtr->name=Alexei stud1.name=Alexei &stud1=134518496 stud1=134514000 &studPtr=134518452 studPtr=134518496 rsmoody@ctec:~/puzzles$ c z7 ~/puzzles rsmoody@ctec:~/puzzles$ z7 studPtr->name  =Alexei stud1.name =   Alexei &stud1   =     134518528 stud1    =     134514000 &studPtr =     134518472 studPtr  =     134518528 rsmoody@ctec:~/puzzles$

code