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

Transcript

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

    and Pointers Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 2 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); }
  3. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 3 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
  4. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 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); }
  5. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 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 | Spring 2018 | 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. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 jgs

    string.h library
  8. jgs Test yourselves

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

    1
  10. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 10 jgs

    2
  11. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 jgs

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

    4
  13. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 13 jgs

    5
  14. jgs What about 2D arrays?

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

    Dynamic 2D Array
  16. 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.