1.5k

# CSE240 Lecture 10

Introduction to Programming Languages
Arrays and Pointers
(202009)

January 10, 2017

## 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

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); }

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

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

Test Yourselves

1

2

3

4

5

Let’s Work

2D Array

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.