Presentation is loading. Please wait.

Presentation is loading. Please wait.

C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.

Similar presentations


Presentation on theme: "C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University."— Presentation transcript:

1 C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University

2 TRU-COMP2130 C: Advanced Topics2 Character Pointers #include gets(), puts() strcpy(), strlen(), strcmp(), strcat(),... toupper(),...

3 TRU-COMP2130 C Programming3 char name[256], tmp[256]; name[0] = C; name[1] = O; name[2] = M; name[3] = P; name[4] = \0;// it is very important. name[5] = ; name[6] = 2; name[7] = 1; name[8] = 3; name[9] = 0; name[10] = \0; // it is very important. printf(course number = %s\n, name); printf(%p\n, name); printf(course number = %s\n, &(name[5])); scanf(%s, name);// not &name sprintf(tmp, course name is %s., name);

4 TRU-COMP2130 C: Advanced Topics4 Pointer Arrays: Pointers to Pointers void f(int *x[13]); // 13 int* variables void f(int (*x)[13]); // pointer to an array of 13 ints // equivalent to int x[][13] Command-line arguments int main(int argc, char *argv[]); argcthe number of arguments argv[0]the program name, e.g., a.out argv[1]the first argument from the user E.g., $./a.out test this comp argc: 4 argv[0]:./a.out argv[1]: test

5 TRU-COMP2130 C: Advanced Topics5 2. Structures User-defined data structure struct student_rcd {// class without methods in Java intstudent_number; charname[128];... };... struct student_rcd record[10], *rp; struct student_rcd test;// how to declare a struct variable test.student_number = 10;// how to access a member print_rcd(test); read_rcd(&test); record[0].student_number = 5; rp = (struct student_rcd *)malloc(sizeof(struct student_rcd) * 3); rp->student_number = 20; // Be careful with the -> (*(rp+1)).student_number = 40; rp[2].student_number = 30;...

6 TRU-COMP2130 C: Advanced Topics6 void print_rcd(struct student_rcd rcd) { printf(Number: %d\n, rcd.student_number); printf(Name: %s\n, rcd.name); // name is an array. // not &(rcd.name) } void read_rcd(struct student_rcd *rcd) { printf(Enter number: ); scanf(%d, &(rcd->student_number)); // reference rqd printf(Enter name: ); scanf(%s, rcd->name); // name is an array. // not &(rcd->name) }

7 TRU-COMP2130 C: Advanced Topics7 Self-Referential Structures struct tnode { /* the tree node: */ char *word; /* points to the text */ int count; /* number of occurrences */ struct tnode *left; /* left child */ struct tnode *right; /* right child */ struct tnode *parent; }; struct tnode root; root.left = (struct tnode *)malloc(...);

8 TRU-COMP2130 C: Advanced Topics8 Typedef typedef int Length; // Now Length is a data type. typedef char *String; // Now String is a data type. typedef struct tnode { /* the tree node: */ char *word; /* points to the text */ int count; /* number of occurrences */ struct tnode *left, *right; /* children */ struct tnode *parent; } Treenode; // Now Treenode is a data type.... Length len, maxlen; Length *lengths[]; String p, lineptr[MAXLINES]; Treenodetnode; p = (String) malloc(100); // p[0],..., p[99]

9 TRU-COMP2130 C: Advanced Topics9 Example struct Student { char name[128]; int number; Student *next; }; typedef struct Student Student; Student *head = null; // important Student *new, *tmp; new = create_student(); // create a record, read data from the user, // store them into the record add_student(head, new); // add the new record at the end of the list tmp = find_student(head, ); // find the record whose number is.. printf(%d: %s\n, tmp->number, tmp->name); // print the record delete_student(head, ); Name Number next Name Number next Name Number next Head null

10 TRU-COMP2130 C: Advanced Topics10... create_student(... ) // create a record, read data from the user, { // store them into the record Student *new = (... )malloc(... ); scanf(%s,...) // read name scanf(%d,...) // read number new->next = null; // very important; (*new).next = null return...; };... add_student(... head,... new) // add the new record at the end of the list { Student *tmp; if (head == null) // when there is no record yet head = new; else { while((*tmp).next != null) // move to the last record tmp = (*tmp).next; // You cannot use array syntaxes (*tmp).next = new; // because the Student objects were not } // consecutively created. return; }

11 TRU-COMP2130 C: Advanced Topics11... find_student(... head,... no) // find the record { Student *tmp; tmp = head; while(tmp != null) { if ((... == no) break; tmp =...; }... delete_student(... head,... no) {... }

12 TRU-COMP2130 C: Advanced Topics12 Unions For polymorphism union u_tag { // the shared storage int ival; float fval; char *sval; }... int utype; union u_tag u;... u.ival = 20; if (utype == INT) printf("%d\n", u.ival); if (utype == FLOAT) printf("%f\n", u.fval); if (utype == STRING) printf("%s\n", u.sval); else printf("bad type %d in utype\n", utype);

13 TRU-COMP2130 C: Advanced Topics13 3. Input and Output Standard input from keyboard $ prog < infileinput redirection $ otherprog | progpipe int getchar() int putchar(int c)

14 TRU-COMP2130 C: Advanced Topics14 Formatted input int scanf (char *format, arg1, arg2,...)// from stdin int sscanf (char *string, char *format, arg1, arg2,...);// from string The arguments must be references. {float f; int i; string the_string = "foo -3.6 fum dum 17"; sscanf(the_string, "foo %f fum dum %d", f, i); } sscanf("foo", "f%s", a);

15 TRU-COMP2130 C: Advanced Topics15 File Access #include FILE *in, *out; // FILE is defined in in = fopen(in_filename, r);// mode: r, w, a, r+, w+, a+ if (in == NULL)... out = fopen(out_filename, w); fclose(in); fprintf(out, format..., variables...); fscanf(...); fgets(...); int fseek(FILE*, long, SEEK_SET or SEEK_CURRENT or SEEK_END); // move file position pointer int fwrite(void*, int memb_size, int no_memb, FILE*); int fread(void*, int memb_size, int no_memb, FILE*);

16 TRU-COMP2130 C: Advanced Topics16 int fputc(int, FILE*); int fputs(char*, FILE*); int fgetc(FILE*); int fscanf(FILE*, char* format,...); int fprintf(FILE*, char* format,...); Examples: A file copy program, using fopen(), fseek(), fwrite(), fread(), fclose(). Files containing student records struct student {... }; struct student record; FILE *fp = fopen(test, w+); // read and write; file truncated; fwrite(&record, sizeof(struct student), 1, fp); fread(&record, sizeof(struct student), 1, fp);

17 TRU-COMP2130 C: Advanced Topics17 How to obtain the current position:: long ftell(FILE*);

18 TRU-COMP2130 C: Advanced Topics18 Error Handling – Stderr and Exit fprintf(stderr, char*,...); exit(int);// non zero means error

19 TRU-COMP2130 C Programming19 math.h Some MATH related functions # include double sqrt(double); double pow(double, double); double fabs(double);... Link with –lm-lm means libm.a, that contains math utilities, is used $ gcc program3-5.c –lm


Download ppt "C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University."

Similar presentations


Ads by Google