Download - C Review Pointers, Arrays, and I/O
![Page 1: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/1.jpg)
C ReviewPointers, Arrays, and I/O
CS61c Summer 2006
Michael Le
![Page 2: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/2.jpg)
C Advice
• Draw stuff out– Variables are boxes, pointers are arrows
• Give a type your variables!
• & returns a value whose type has one more star than the type of the variable– int quux; int* baz = &quux;
• Execute the fundamental operations one at a time– variable lookup, pointer deference, etc
![Page 3: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/3.jpg)
Tracing Pointers – Warm Up
What will y contain?
int main(int argc, char** argv){ int y, *z; y = 4; z = &y; y = *z + 9; return 0;}
![Page 4: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/4.jpg)
Tracing Pointers – Warm Up
What will y contain?
int main(int argc, char** argv){ int y, *z; y = 4; z = &y; y = *z + 9; return 0;}
int y
int* z
![Page 5: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/5.jpg)
Tracing Pointers – Warm Up
What will y contain?
int main(int argc, char** argv){ int y, *z; y = 4; z = &y; y = *z + 9; return 0;}
0x4
int y
int* z
![Page 6: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/6.jpg)
Tracing Pointers – Warm Up
What will y contain?
int main(int argc, char** argv){ int y, *z; y = 4; z = &y; y = *z + 9; return 0;}
0x4
int y
int* z
![Page 7: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/7.jpg)
Tracing Pointers – Warm Up
What will y contain?
int main(int argc, char** argv){ int y, *z; y = 4; z = &y; y = *z + 9; return 0;}
0xD
int y
int* z
![Page 8: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/8.jpg)
Tracing Pointers – Warm Up
What will y contain?
int main(int argc, char** argv){ int y, *z; y = 4; z = &y; y = *z + 9; return 0;}
0xD
int y
int* z
It contains 0xD. What is that in binary? In decimal?
![Page 9: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/9.jpg)
Tracing Pointers – More Levels
What is in foo and bar at the end of this program?
int main(int argc, char** argv){ int foo, *bar, **baz, quux; bar = &quux; foo = 4; baz = &bar; **baz = 13; bar = &foo; **baz = 9;
return 0;}
![Page 10: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/10.jpg)
Tracing Pointers – More Levels
What is in foo and quux at the end of this program?
int main(int argc, char** argv){ int foo, *bar, **baz, quux; bar = &quux; foo = 4; baz = &bar; **baz = 13; bar = &foo; **baz = 9;
return 0;}
410
int foo int quux
int* bar
int** baz
![Page 11: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/11.jpg)
Tracing Pointers – More Levels
What is in foo and quux at the end of this program?
int main(int argc, char** argv){ int foo, *bar, **baz, quux; bar = &quux; foo = 4; baz = &bar; **baz = 13; bar = &foo; **baz = 9;
return 0;}
410
int foo
1310
int quux
int* bar
int** baz
![Page 12: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/12.jpg)
Tracing Pointers – More Levels
What is in foo and quux at the end of this program?
int main(int argc, char** argv){ int foo, *bar, **baz, quux; bar = &quux; foo = 4; baz = &bar; **baz = 13; bar = &foo; **baz = 9;
return 0;}
410
int foo
1310
int quux
int* bar
int** baz
![Page 13: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/13.jpg)
Tracing Pointers – More Levels
What is in foo and quux at the end of this program?
int main(int argc, char** argv){ int foo, *bar, **baz, quux; bar = &quux; foo = 4; baz = &bar; **baz = 13; bar = &foo; **baz = 9;
return 0;}
910
int foo
1310
int quux
int* bar
int** baz
![Page 14: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/14.jpg)
What’s wrong with this program?
int modifyCount(int x)
{
x = x – 1;
}
int main(int argc, char** argv)
{
int x = 4;
/* want to change x */
modifyCount(x);
return 0;
}
![Page 15: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/15.jpg)
What’s wrong with this program?
int modifyCount(int x)
{
x = x – 1;
}
int main(int argc, char** argv)
{
int x = 4;
/* want to change x */
modifyCount(x);
return 0;
}
410
int x
![Page 16: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/16.jpg)
What’s wrong with this program?
int modifyCount(int x)
{
x = x – 1;
}
int main(int argc, char** argv)
{
int x = 4;
/* want to change x */
modifyCount(x);
return 0;
}
410
int x
410
int x
![Page 17: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/17.jpg)
What’s wrong with this program?
int modifyCount(int x)
{
x = x – 1;
}
int main(int argc, char** argv)
{
int x = 4;
/* want to change x */
modifyCount(x);
return 0;
}
410
int x
310
int x
![Page 18: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/18.jpg)
What’s wrong with this program?
int modifyCount(int x)
{
x = x – 1;
}
int main(int argc, char** argv)
{
int x = 4;
/* want to change x */
modifyCount(x);
return 0;
}
410
int x
We never changed x! How do we fix this?
![Page 19: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/19.jpg)
Use Pointers!
int modifyCount(int* x)
{
*x = *x – 1;
}
int main(int argc, char** argv)
{
int x = 4;
/* want to change x */
modifyCount(&x);
return 0;
}
![Page 20: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/20.jpg)
What’s wrong with this program?
int modifyCount(int* x)
{
*x = *x – 1;
}
int main(int argc, char** argv)
{
int x = 4;
/* want to change x */
modifyCount(&x);
return 0;
}
410
int x
int* x
![Page 21: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/21.jpg)
Pointers and ++/--
Suppose we have the following program:
int main(int argc, char** argv){ int i, j; int* p = &argc; /* argc = 1 */ i = (*p)++; argc = 1; j = ++(*p); return 0;}
What is in i and j?
![Page 22: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/22.jpg)
Pointers and ++/--
Assuming x and y have type int…
• y = x++; is equivalent to y=x; x=x+1;
• y = ++x; is equivalent to x=x+1; y=x;
![Page 23: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/23.jpg)
Pointers and ++/--
Suppose we have the following program:
int main(int argc, char** argv){ int i, j; int* p = &argc; /* argc = 1 */ i = (*p)++; argc = 1; j = ++(*p); return 0;}
What is in i and j? i = 1 and j = 2
i = *p; *p = *p + 1;
*p = *p + 1;j = *p;
![Page 24: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/24.jpg)
Pointers and []
• x[i] can always be rewritten as *(x+i) and vice versa
• Array types can often be converted into their corresponding pointer counterparts– int foo[] is equivalent to int* foo– int* bar[] is equivalent to int** bar– You can at most change one set of [] safely
• Changing more requires knowing how the array looks in memory
![Page 25: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/25.jpg)
Pointers and ++/--
Suppose we have the following program:
int main(int argc, char** argv){ int i, j; int* p = &argc; /* argc = 1 */ i = (*p)++; argc = 0; j = ++(*p); return 0;}
What is in i and j? Both contain 1
i = *p; *p = *p + 1;
*p = *p + 1;j = *p;
![Page 26: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/26.jpg)
printf, scanf, and their cousins
• printf (and its cousins) are special functions that do not have a fixed argument list– for each format specifier (i.e. %d), an
additional argument needs to be supplied
• Examples:– printf(“%d”, 4);– printf(“%s%d%c”, “CS”, 0x3D, ‘c’);
![Page 27: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/27.jpg)
printf, scanf, and their cousins
• Unlike printf, with scanf for each format specifier (i.e. %d), an additional argument needs to be supplied that has type pointer
• Examples:– int z; scanf(“%d”, &z);– char foo[5]; int d;
scanf(“%s %d”, foo, &d);
![Page 28: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/28.jpg)
C Program WalkthroughWhat happens with this program?
void quux(int foo) { char a[4]; char* baz = (char*)(&foo); printf("%c%c%c%c", baz[0], *(baz + 1), baz[1+1], baz[sprintf(a, "123")]); }
int main(...) { quux(0x4d495053); }
![Page 29: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/29.jpg)
C Program WalkthroughWhat happens with this program?
void quux(int foo) { char a[4]; char* baz = (char*)(&foo); printf("%c%c%c%c", baz[0], *(baz + 1), baz[1+1], baz[sprintf(a, "123")]); }
int main(...) { quux(0x4d495053); }
0x4d495053
int foo
![Page 30: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/30.jpg)
C Program WalkthroughWhat happens with this program?
void quux(int foo) { char a[4]; char* baz = (char*)(&foo); printf("%c%c%c%c", baz[0], *(baz + 1), baz[1+1], baz[sprintf(a, "123")]); }
int main(...) { quux(0x4d495053); }
0x4d495053
int foo
char* baz
![Page 31: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/31.jpg)
C Program WalkthroughWhat happens with this program?
void quux(int foo) { char a[4]; char* baz = (char*)(&foo); printf("%c%c%c%c", baz[0], *(baz + 1), baz[1+1], baz[sprintf(a, "123")]); }
int main(...) { quux(0x4d495053); }
0x4d495053
int foo
char* baz
![Page 32: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/32.jpg)
C Program WalkthroughWhat happens with this program?
void quux(int foo) { char a[4]; char* baz = (char*)(&foo); printf("%c%c%c%c", baz[0], *(baz + 1), baz[1+1], baz[sprintf(a, "123")]); }
int main(...) { quux(0x4d495053); }
0x4d495053
int foo
4d 49 50 53
char* baz
![Page 33: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/33.jpg)
C Program WalkthroughWhat happens with this program?
void quux(int foo) { char a[4]; char* baz = (char*)(&foo); printf("%c%c%c%c", baz[0], *(baz + 1), baz[1+1], baz[sprintf(a, "123")]); }
int main(...) { quux(0x4d495053); }
0x4d495053
int foo
4d 49 50 53
char* baz
![Page 34: C Review Pointers, Arrays, and I/O](https://reader035.vdocuments.net/reader035/viewer/2022070405/56813ffd550346895dab2b5b/html5/thumbnails/34.jpg)
C Program WalkthroughWhat happens with this program?
void quux(int foo) { char a[4]; char* baz = (char*)(&foo); printf("%c%c%c%c", baz[0], *(baz + 1), baz[1+1], baz[sprintf(a, "123")]); }
int main(...) { quux(0x4d495053); }
0x4d495053
int foo
4d 49 50 53
char* baz
It will print out “MIPS”