Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE240 Lecture 12

CSE240 Lecture 12

Introduction to Programming Languages
Struct
(202009)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

January 12, 2017
Tweet

Transcript

  1. jgs CSE 240 Introduction to Programming Languages Lecture 12: Struct

    Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
  2. jgs What about 2D arrays?

  3. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 3 jgs

    2D Array
  4. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 4 jgs

    malloc and 2D arrays #include <stdio.h> #include <stdlib.h> int main(){ int **array = (int**) malloc (sizeof(int*) * 3); *(array+0) = (int*) malloc (sizeof(int ) * 3); array[1] = (int*) malloc (sizeof(int ) * 3); *(array+2) = (int*) malloc (sizeof(int ) * 3); array[0][0] = 1; array[1][1] = 1; array[2][2] = 1; *(*(array +1 ) + 2) = 3; // this is array[1][2] printf ("array [1][2] is %d \n", array[1][2]); printf ("array [2][2] is %d \n", *(*(array+2)+2) ); // free (s) go here return 0; }
  5. jgs Test Yourselves

  6. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 6 jgs

    Dynamic 2D Array
  7. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 jgs

    Dynamic 2D Array
  8. jgs typedef and enum

  9. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 9 jgs

    typedef § typedef typename newname; § introduces a new name that becomes a synonym for the type given by the typename portion of the declaration. typedef int booOoolean; typedef char FlagType; int main() { booOoolean x = 0; int counter FlagType y = 'A'; // etc. }
  10. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 10 jgs

    typedef § typedef typename newname; § introduces a new name that becomes a synonym for the type given by the typename portion of the declaration. typedef int booOoolean; typedef char FlagType; int main() { booOoolean x = 0; int counter FlagType y = 'A'; // etc. }
  11. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 jgs

    typedef and enum § enum allow us to define the allowed values for a new type § The elements in an enumeration are integer constants, i.e., they are labels that represent an integer value typedef enum {false, true} booOoolean; typedef enum {Sun, Mon, Tue, Wed, Thu, Fri, Sat} days; int main() { booOoolean a = false; int counter; days x = Mon, y = Fri; days today = x + y; printf("%d", today); }
  12. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 jgs

    typedef and enum #include <stdio.h> typedef enum { red, amber, green} traffic_light; typedef enum { No, Yes} logic; void main( ) { traffic_light x = red; logic var = Yes; while (var == Yes) { switch (x) { case amber: x = red; printf("Red Light"); break; case red: x = green; printf("Green Light"); break; case green: x = amber; printf("Amber Light"); break; } var = No; } }
  13. jgs struct

  14. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 14 jgs

    struct type § A structure is is a composite data type declaration that defines a physically grouped list of variables to be placed under one name in a block of memory. § It is created by the keyword struct. § Similar to a Java class; but DOES NOT allow methods
  15. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 15 jgs

    struct type struct type_name { type1 element1; type2 element2; . . . typen elementn; }; struct type_name a, b; // Example #include <stdio.h> struct person { char name[30]; int id; }; void main( ) { struct person x, y; scanf("%s", x.name ); scanf("%d", &x.id); printf("%s", x.name); printf("\n"); printf("%d", x.id); }
  16. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 16 jgs

    Arrays of struct #include <stdio.h> struct contact { char name[30]; int phone; char email[30]; }; struct contact contact_book[100]; // an array of structures void main() { int index = 0; scanf("%d", &contact_book[index].phone); scanf("%s", contact_book[index].name ); scanf("%s", contact_book[index].email); printf("\n %d", contact_book[index].phone); printf("\n %s", contact_book[index].name); printf("\n %s", contact_book[index].email); }
  17. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 17 jgs

    Pointers to struct name phone email x struct contact { char name[30]; int phone; char email[30]; }; main () { struct contact x; struct contact *y; scanf("%s", x.name); //dot with x scanf("%d", &x.phone); scanf("%s", x.email); printf ("%d \n", x.phone); y = &x; (*y).phone = 101010; //asterisk and dot with y y->phone = 404040; // arrow with y printf ("%d \n", y->phone); } y
  18. jgs What is next?

  19. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 19 jgs

    Linked List #include <stdlib.h> struct node { int val; struct node * next; }; struct node * head = NULL; head = malloc(sizeof(struct node )); head->val = 1; head->next = malloc(sizeof(struct node )); head->next->val = 2; head->next->next = NULL; struct node * head = NULL; head = malloc(sizeof(struct node)); if (head == NULL) { return 1; } head->val = 1; head->next = NULL; void print_list(struct node * head) { node_t * current = head; while (current != NULL) { printf("%d\n", current->val); current = current->next; } }
  20. jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu

    Spring 2018 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.