Course organization. Course introduction ( Week 1)

Similar documents
Course organization. Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4)

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Arrays and Pointers (part 1)

Language comparison. C has pointers. Java has references. C++ has pointers and references

Arrays and Pointers (part 2) Be extra careful with pointers!

Arrays and Pointers (part 2) Be extra careful with pointers!

Arrays and Pointers (part 1)

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

Arrays, Pointers and Memory Management

Course organization. Course introduction ( Week 1)

Pointers. Pointers. Pointers (cont) CS 217

Memory, Data, & Addressing II CSE 351 Spring

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Systems Programming and Computer Architecture ( )

Arrays and Pointers in C. Alan L. Cox

Pointers. Pointer References

primitive arrays v. vectors (1)

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Course organization. Course introduction ( Week 1)

Array Initialization

FUNCTIONS POINTERS. Pointers. Functions

PRINCIPLES OF OPERATING SYSTEMS

Pointers. Memory. void foo() { }//return

211: Computer Architecture Summer 2016

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Lecture 04 Introduction to pointers

Fundamental of Programming (C)

Character Strings. String-copy Example

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Today s class. Review of more C Operating system overview. Informationsteknologi

Course organization. Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4)

Lab 3. Pointers Programming Lab (Using C) XU Silei

Binghamton University. CS-211 Fall Pointers

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

CS C Primer. Tyler Szepesi. January 16, 2013

Course organization Course introduction ( Week 1) Part I: Introduction to C programming language (Week 3-12)

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Pointers, command line arguments, Process control, and functions

Binghamton University. CS-211 Fall Variable Scope

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Intermediate Programming, Spring 2017*

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

More Pointers Week 11

Intermediate Programming, Spring 2017*

Dynamic arrays / C Strings

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Understanding Pointers

Dynamic memory allocation

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Dynamic memory allocation (malloc)

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Summer May 18, 2010

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Programming in C. Pointers and Arrays

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Class Information ANNOUCEMENTS

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Memory Allocation in C

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

C++ for Java Programmers

EM108 Software Development for Engineers

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Final CSE 131B Spring 2005

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

Basic and Practice in Programming Lab7

CS 241 Data Organization Pointers and Arrays

[0569] p 0318 garbage

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

SYSC 2006 C Winter 2012

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Quick review pointer basics (KR ch )

DECLARAING AND INITIALIZING POINTERS

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CSC 2400: Computer Systems. Arrays and Strings in C

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Overview (1A) Young Won Lim 9/25/17

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Memory, Data, & Addressing II

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

CSC 2400: Computer Systems. Arrays and Strings in C

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Arrays, Strings, & Pointers

CS 107 Lecture 5: Arrays. and Pointers in C. Monday, January 22, Stanford University. Computer Science Department

Transcription:

Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and expressions (Week 4) Chapter 3: Control flow (Week 5) Chapter 4: Functions and program structure (Week 6, 7) Chapter 5: Pointers and arrays (Week 8) Chapter 6: Structures (Week 9) Chapter 7: I/O (Week 10) Part II: Skills others than programming languages (Week 11-12) Debugging tools(week 11) Keeping projects documented and manageable (Week 12) Source code managing (Week 12) Part III: Reports from the battle field (student forum) (week 12 16) 1

Chapter 5. Points and Arrays Chaochun Wei Shanghai Jiao Tong University Spring 2013

Contents 5.1 Pointers and addresses 5.2 Pointers and function arguments 5.3 Pointers and arrays 5.4 Address arithmetic 5.5 Character pointers and functions 5.6 Pointer arrays, pointers to pointers 5.7 Multi-dimensional arrays 5.8 Initialization of pointer arrays 5.9 Pointers vs. multi-dimensional arrays 5.10 Command-line arguments 5.11 Pointers to functions 5.12 Complicated declarations

5.1 Pointers and address For any type T, you may form a pointer type to T. Pointers may reference a function or an object. The value of a pointer is the address of the corresponding object or function Examples: int *i; char *x; int (*myfunc)(); Pointer operators: * dereferences a pointer, & creates a pointer (reference to) int i = 3; int *j = &i; *j = 4; printf( i = %d\n, i); // prints i = 4 int myfunc (int arg); int (*fptr)(int) = myfunc; i = fptr(4); // same as calling myfunc(4); Generic pointers: Traditional C used (char *) Standard C uses (void *) these can not be dereferenced or used in pointer arithmetic. So they help to reduce programming errors Null pointers: use NULL or 0. It is a good idea to always initialize pointers to NULL.

5.1 Pointers and address Step 1: int main (int argc, argv) { int x = 4; int * y = &x;... x y Program Memory 4 0x3dc Address 0x3dc 0x3d8 0x3d4 0x3d0 0x3cc 0x3c8 0x3c4 0x3c0 0x3bc 0x3b8 0x3b4 0x3b0

5.1 Pointers and address More example operations on pointers int x = 1, y = 2; int *ip; ip = &x; /* ip points to x */ y = *ip; /* y = 1; */ *ip = *ip + 10; /* equivalent to x = x + 10; */ y= *ip +1; /* note the difference with *ip += 1 */ ++ *ip; /* similar to *ip += 1 and (*ip) ++ */ (See more details in hands-on experiment 5.1)

More details see hands-on experiments 5.2 5.2 Pointers and function arguments Arguments are passed to functions by value. /* function to swap the values of two variable */ int a = 1, b = 2; swap(a, b); void swap (int x, int y) { int temp; temp = x; x = y; y = temp; } int a = 1, b = 2; swap(&a, &b); WRONG!!! void swap (int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; }

5.3 Arrays and Pointers A variable declared as an array represents a contiguous region of memory in which the array elements are stored. int x[5]; // an array of 5 4-byte ints. All arrays begin with an index of 0 An array identifier is equivalent to a pointer that references the first element of the array int x[5], *ptr; ptr = &x[0] is equivalent to ptr = x; Pointer arithmetic and arrays: int x[5]; x[2] is the same as *(x + 2), the compiler will assume you mean 2 objects beyond element x. 0 1 2 3 4 little endian byte ordering 0 1 2 3 memory layout for array x

5.3 Arrays and pointers (continued I) Step 1: int main (int argc, argv) { int x = 4; int *y = &x; int *z[4] = {NULL, NULL, NULL, NULL}; int a[4] = {1, 2, 3, 4};... Note: The compiler converts z[1] or *(z+1) to Value at address (Address of z + sizeof(int)); In C you would write the byte address as: (char *)z + sizeof(int); or letting the compiler do the work for you (int *)z + 1; x y z[3] z[2] z[1] z[0] a[3] a[2] a[1] a[0] Program Memory 4 0x3dc 0 0 0 0 4 3 2 1 Address 0x3dc 0x3d8 0x3d4 0x3d0 0x3cc 0x3c8 0x3c4 0x3c0 0x3bc 0x3b8 0x3b4 0x3b0

5.3 Arrays and pointers (Continued II) Step 1: int main (int argc, argv) { int x = 4; int *y = &x; int *z[4] = {NULL, NULL, NULL, NULL}; int a[4] = {1, 2, 3, 4}; Step 2: Assign addresses to array Z z[0] = a; // same as &a[0]; z[1] = a + 1; // same as &a[1]; z[2] = a + 2; // same as &a[2]; z[3] = a + 3; // same as &a[3]; x y z[3] z[2] z[1] z[0] a[3] a[2] a[1] a[0] Program Memory 4 0x3dc 0x3bc 0x3b8 0x3b4 0x3b0 4 3 2 1 Address 0x3dc 0x3d8 0x3d4 0x3d0 0x3cc 0x3c8 0x3c4 0x3c0 0x3bc 0x3b8 0x3b4 0x3b0

5.3 Arrays and pointers (Continued III) Step 1: int main (int argc, argv) { int x = 4; int *y = &x; int *z[4] = {NULL, NULL, NULL, NULL}; int a[4] = {1, 2, 3, 4}; Step 2: z[0] = a; z[1] = a + 1; z[2] = a + 2; z[3] = a + 3; Step 3: No change in z s values z[0] = (int *)((char *)a); z[1] = (int *)((char *)a + sizeof(int)); z[2] = (int *)((char *)a + 2 * sizeof(int)); z[3] = (int *)((char *)a + 3 * sizeof(int)); x y z[3] z[2] z[1] z[0] a[3] a[2] a[1] a[0] Program Memory 4 0x3dc 0x3bc 0x3b8 0x3b4 0x3b0 4 3 2 1 Address 0x3dc 0x3d8 0x3d4 0x3d0 0x3cc 0x3c8 0x3c4 0x3c0 0x3bc 0x3b8 0x3b4 0x3b0

See hands-on experiments for more details 5.4 Address arithmetic Pointers can do arithmetic operation +, -, ++ ==,!=, <, >, >=, etc Example: let p, and q be two pointers to an array p++ p+= 1 p < q p + n /* next n object p points to */

End 4.16

5.5 Character pointers and functions String constant: an array of characters, ending with \0 char *pmessage = now is the time ; /* The pointer to the character array is assigned to pmessage. */ char amessage[ ] = now is the time; /* an array */ pmessage: now is the time\0 amessage: now is the time\0

5.5 Character pointers and functions Assignment: is not a string copy operation char *s = this is a string, *t; t = s ; /* this is not a string copy */ /* this copies to t the address that s points to */ To copy a string, we need a loop /* strcpy: copy t to s */ void strcpy(char *s, char *t) { while (( *s++ = *t++)!= \0 ) ; }

5.6 Pointer arrays; pointers to pointers Pointers are variables can be stored in arrays Example: student name list: a 2 dimension array, which can be a pointer array; Pointer array char* name Huang piao\0 Wang hai\0 Cai zhi\0 Sort ore details in hands-on experiments 5.6 Huang piao\0 Wang hai\0 Cai zhi\0

5.7 Multi-dimensional arrays Array of pointers flexible Multi-dimensional arrays Rectanglar, therefore inflexible

5.9 Pointers vs. multi-dimensional arrays Definition: int a[10][20]; int *b[10]; The following two expression are both legal. a[3][4]; b[3][4]; The size of a is 10*20 = 200 The size of b is flexible.

5.10 Command-line arguments main function has two arguments Argc: argument count Argv: argument vector Example /* echo comman-line arguments */ main(int argc, char *argv[ ]) { int i; for (i = 1; i < argc; i ++ ) printf("%s%s", argv[i], (i < argc -1)? " ": ""); printf("\n"); return 0; }