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

CSE240 Lecture 10

CSE240 Lecture 10

Introduction to Programming Languages
Arrays and Pointers
(202009)

Javier Gonzalez-Sanchez
PRO

January 10, 2017
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSE 240
    Introduction to Programming Languages
    Lecture 10: Arrays and Pointers
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. jgs
    Previously …

    View Slide

  3. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 3
    jgs
    Arrays are Pointers
    § An array is a pointer to a set of consecutive elements
    a[0] is the same that *(a+0)
    a[1] is the same that *(a+1)
    a[2] is the same that *(a+2)
    a[3] is the same that *(a+3)
    etc.
    int a [6];
    0x48
    a
    0x44 0x48

    View Slide

  4. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 4
    jgs
    Arrays are Pointers
    #include
    void main() {
    int i = 0;
    char a[ ] = "Hello CSE 240";
    printf("\n message: %s\n ", a);
    while (a[i] != '\0') { a[i] = *(a + i)+1; i++;}
    printf("\n message after encryption: %s\n ", a);
    char *q;
    q = a;
    while (*q != '\0') { *q = *q-1; q++;}
    printf("\n message after decryption: %s\n ", a);
    }
    a[i] = *(a + i)+ 1
    a[i] = a[i]+ 1
    *(a+i) = a[i]+ 1
    *(a+i) = *(a+i)+ 1

    View Slide

  5. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 5
    jgs
    Arrays are Pointers
    #include
    void main() {
    int i = 0;
    char a[ ] = "Hello CSE 240";
    printf("\n message: %s\n ", a);
    while (a[i] != '\0') { a[i] = *(a + i)+1; i++;}
    printf("\n message after encryption: %s\n ", a);
    char *q;
    q = a;
    while (*q != '\0') { *q = *q-1; q++;}
    printf("\n message after decryption: %s\n ", a);
    }
    0x48
    a
    0x44
    H
    0x48
    e l l ...
    0x48
    q
    0x94

    View Slide

  6. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 6
    jgs
    Arrays are Pointers
    #include
    void main() {
    int i = 0;
    char a[ ] = "Hello CSE 240";
    printf("\n message: %s\n ", a);
    while (a[i] != '\0') { a[i] = *(a + i)+1; i++;}
    printf("\n message after encryption: %s\n ", a);
    char *q;
    q = a;
    while (*q != '\0') { *q = *q-1; q++;}
    printf("\n message after decryption: %s\n ", a);
    }

    View Slide

  7. jgs
    Arrays, Strings, and Pointers

    View Slide

  8. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 8
    jgs
    Test Yourselves

    View Slide

  9. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 9
    jgs
    Test Yourselves

    View Slide

  10. jgs
    Test yourselves

    View Slide

  11. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 11
    jgs
    1

    View Slide

  12. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 12
    jgs
    2

    View Slide

  13. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 13
    jgs
    3

    View Slide

  14. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 14
    jgs
    4

    View Slide

  15. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 15
    jgs
    5

    View Slide

  16. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 16
    jgs
    Let’s Work

    View Slide

  17. jgs
    What about 2D arrays?

    View Slide

  18. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 18
    jgs
    2D Array

    View Slide

  19. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 19
    jgs
    Questions

    View Slide

  20. jgs
    CSE 240 Introduction to Programming Languages
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Fall 2021
    Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University.
    They cannot be distributed or used for another purpose.

    View Slide