C Review Pointers, Arrays, and I/O CS61c Summer 2006 Michael Le.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
Kernighan/Ritchie: Kelley/Pohl:
Stack buffer overflow.
Stack buffer overflow
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Advanced Programming in the UNIX Environment Hop Lee.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
CS1061 C Programming Lecture 15: More on Characters and Strings A. O’Riordan, 2004.
CS61C L4 C Pointers (1) Chae, Summer 2008 © UCB Albert Chae Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 –C Strings,
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
CSE 332: C++ functions Review: What = and & Mean In C++ the = symbol means either initialization or assignment –If it’s used with a type declaration, it.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Return function Random function Recursion function Function in C 1.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Command Line Arguments plus Variable-Length Arrays Systems Programming.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
File IO and command line input CSE 2451 Rong Shi.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Cryptography.
System Programming Practical Session 7 C++ Memory Handling.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
C Primer.
A bit of C programming Lecture 3 Uli Raich.
Characters and Strings
Command Line Arguments
Agenda Make Utility Command Line Arguments in Unix
Command Line Arguments
Chapter 3: I/O Management
Command-Line Arguments
Stack buffer overflow.
Programming in C Pointer Basics.
Procedure Activations
Programming in C Pointer Basics.
Dynamic Memory A whole heap of fun….
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Programming in C Pointers and Arrays.
The Stack.
Functions Reasons Concepts Passing arguments to a function
15213 C Primer 17 September 2002.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2010.
Presentation transcript:

C Review Pointers, Arrays, and I/O CS61c Summer 2006 Michael Le

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

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

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

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

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

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

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?

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

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; } 4 10 int foo int quux int* bar int** baz

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; } 4 10 int foo int quux int* bar int** baz

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; } 4 10 int foo int quux int* bar int** baz

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; } 9 10 int foo int quux int* bar int** baz

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

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; } 4 10 int x

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; } 4 10 int x 4 10 int x

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; } 4 10 int x 3 10 int x

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; } 4 10 int x We never changed x ! How do we fix this?

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

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; } 4 10 int x int* x

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 ?

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;

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; j = *p;

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

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; j = *p;

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

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

C Program Walkthrough What 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); }

C Program Walkthrough What 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); } 0x4d int foo

C Program Walkthrough What 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); } 0x4d int foo char* baz

C Program Walkthrough What 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); } 0x4d int foo char* baz

C Program Walkthrough What 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); } 0x4d int foo 4d char* baz

C Program Walkthrough What 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); } 0x4d int foo 4d char* baz

C Program Walkthrough What 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); } 0x4d int foo 4d char* baz It will print out “MIPS”