Structures and Pointers C + GNU HTML Perl Python Comp-206 : Introduction to Software Systems Week 7

Structures and Pointers C + GNU HTML Perl Python Comp-206 : Introduction to Software Systems Week 7

1 Week 7 Week Joseph Vybihal Computer ScienceComputer McGill UniversityMcGill Structures and Pointers Comp-206 : Introduction :Comp-206 toSoftwareSystems COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 2 Announcements Programming (1 or 2) Syntax precision (0 to 1) Definition (0 to 1) – – – C programming About the Unix operatingsystem / history Programming and and syntaxProgramming based (>95% accuracy) Unix command-line commands Bash programming 3 questions • • • • • • Topics 1hourexam Includes 7(half)lectures: 1 to Week Week – – – Midterm exam content • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 3 Part 1 C Structures COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 4 keyword. struct FIELDS; struct TYPENAME { } VARNAME; Structures and Unions int number_of_students; char name_professor[100]; char location_building[100]; int location_room; Think of it as a container, a variable that has variables inside it. } cs206; struct course_record { – types. canYou define new structures using the Structures are a data type several of composed other data • • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 5 Using a structure 328}; struct course cs206 = {60, “Alex”, “MacDonald”, cs206.location_room = 328; cs206.number_of_student = 60; cs206.name_professor = “Alex”; cs206.location_building = “MacDonald”; struct course cs206; With structures,With you can declare the variable and initialize it with datain one command. You canYou then fill it with data. To useTo a structure, you need to instantiate a it. copy of All you need to isdo todeclare the variablefor the instance. • • • • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 6 typedef and struct int number_of_students; char name_professor[100]; char location_building[100]; int location_room; TYPENAME cs206; } TYPENAME; typedef struct course { When creatingWhen a variable this of type, you no longer need to specify the struct keyword. You canYou use typedef to define thestructure as a new type. • • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 7 Question And use it?And – How to build an array of student structures? • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 8 Answer charname[100]; int age; float gpa; for(i=0;i<50;i++) printf(“%s”,students[i].name); gets(student[2].name); } students[50]; students[2].age = 20; struct student_rec { COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 9 How doesHow this look in RAM? Polymorphism in C int x; chary; float d; FIELDS; } z; { union multiple } VAR; union TYPENAME { COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 10 Question Staffs Students Teachers – – – How can we build a polymorphic data structure with: • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 11 Answer struct teacher_rec teacher; struct stud_rec student; struct staff_rec staff; How canHow we tell the difference between records? } school[100]; { struct staff_rec { ... }; union school_rec struct teacher_rec { .... }; struct stud_rec { ... }; COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 12 Part 2 Enumerated Types COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 13 Enumerated Types saturday = 60, sunday = 0}; enum days {monday = 1, tuesday, wednesday, thursday, friday, saturday, sunday}; enum days {monday = 10, tuesday = 20, wednesday = 30, thursday = 40, friday = 50, enum days {monday, tuesday, wednesday, thursday, friday, saturday, sunday}; So monday has value 0, tuesday 1, ... – – – – Or simplyOr assign different numerical values: We canWe also override the 0 start value: As withAs arrays first enumerated has name index value0. Enumerated types : contain a list constants of that can be addressed in integer values. • • • • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 14 {monday = 1, Using Enumerations typedef enum days tuesday, wednesday, thursday, friday, saturday, sunday}; days week1; enum days week1; enum – – – If you typedef an enumerated type, you can use it without the enum keyword. similar to a structure: Creating a variable of an enumeration is • • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 15 Example How is this similar to integer indexes? then incrementing through the array Defining an array with enumerated days and • • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 16 Answer for(i=monday; i<=sunday; i++) array[i]; daysarray[7]; COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 17 Part 3 Dynamic Memory COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 18 block of memory. block memory. of It will contain whatever is currently in memory. Nothing will prevents accessing you from outside that void *malloc(size_t size); void *calloc(int repeat,size_t size); Dynamic Memory Allocation – – – – allocated. Be careful not to access memory outsideyouwhat carefulnot memory Be to access The size ofblock size the be specified.The must is not initialized.block That memory The malloc() function The block allocates of a memory andreturns a pointer to thatallocated memory. • • • • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 19 Using the blocks of memory int *a = (int *) malloc( sizeof(int) * 40 ); type. The sizeof()The function simplifiesthe allocation of bycalculating the of provided size the memory data This generic block of memory must be cast beforebe cast it must This generic block of memory be used. can (void *). you void*In use a C, when return generic pointer. a Both malloc and calloc return and calloc voidmalloc Both a pointer • • • • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 20 There is no garbage collector in C… Deallocating Memory continually allocate memory. Otherwise, undefined behaviour occurs. This canbe an especially serious problem if you void free(void *ptr); – – – Not releasing memory afterfinishing Not releasing memory with it can leaks. memory create a previousa calloc()realloc(). call to or malloc(), The specified memory must have been returnedhave been by must specifiedThe memory The free()function The specified thememory releases space. • • • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 21 Example What about strdup()? – Building my own struct... Building my own string… Building my own array… • • • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 22 Answers struct student_rec *ptr = malloc(sizeof(struct student_rec)); char= *s malloc(100); int *p = calloc(10,sizeof(int)); COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 23 Part 4 Data Structures COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix Data Structures in C Linked List Tree Binary – – pointers, let's take a look at two data structures. To better understand the importance of • COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix 3 null 10 6 Linked List 8 Head COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix Link List Node typedef names:llnode linkedlist and int value; struct ll_node* next; } llnode, } linkedlist; typedef struct { typedef ll_node /* for */ /* Node the link list COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix Creating a node // dummy node at the head. head = (llnode *)malloc(sizeof(llnode)); head->value = 0; head->next = NULL; return head; llnode* head; // Our implementation of linklist has a } /* Create a */ /* Create link list { linkedlist* createLinkedList() COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix freeSpot = freeSpot->next; Adding a Node freeSpot->next = newNode; } newNode = (llnode *)malloc(sizeof(llnode)); newNode->value = value; newNode->next = NULL; llnode* freeSpot; llnode* newNode; // Find a free spot at the end to add the value freeSpot = list; while(freeSpot->next != NULL) { * Add a value to the linklist. */ } / void addToLinkedList(linkedlist* list, int value) { COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix Pretty Print printf("Content of listis:"); printLLNode(list->next); printf("List is empty."); } else { } printf("\n"); if (list->next != NULL) { * Pretty print the list. */ } / void printLinkedList(linkedlist* list) { COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix printLLNode(my_node->next); Printing A Node } printf(" %i ", my_node->value); if (my_node->next != NULL) { } void my_node){ void printLLNode(llnode* COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix null 10 null 8 Binary Tree 6 head 3 COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix Binary Tree Node struct bt_node* left; struct bt_node* right; char* value; } btnode, } binarytree; /* inour /* Node binary tree */ struct { typedef bt_node COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix Creating the Tree return createBTNode(value); } binarytree* createBinaryTree(char* binarytree* createBinaryTree(char* value) { COMP 206 - Joseph Vybihal 2007 Software Systems Python Perl HTML C C + GNU Unix Creating

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    39 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us