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)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

January 10, 2017
Tweet

More Decks by Javier Gonzalez

Other Decks in Programming

Transcript

  1. jgs CSE 240 Introduction to Programming Languages Lecture 10: Arrays

    and Pointers Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs Previously …

  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
  4. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 4 jgs

    Arrays are Pointers #include <stdio.h> 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
  5. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 5 jgs

    Arrays are Pointers #include <stdio.h> 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
  6. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 6 jgs

    Arrays are Pointers #include <stdio.h> 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); }
  7. jgs Arrays, Strings, and Pointers

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

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

    Test Yourselves
  10. jgs Test yourselves

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

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

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

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

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

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

    Let’s Work
  17. jgs What about 2D arrays?

  18. Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 18 jgs

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

    Questions
  20. jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez, Ph.D.

    javiergs@asu.edu 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.