ECE 382 Lesson 21 Readings Lesson Outline

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
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”
Lecture 12: Pointers B Burlingame 25 Nov Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
ECE 382 Lesson 20 Lesson Outline Structs Functions Headers Example
EGR 2261 Unit 11 Pointers and Dynamic Variables
Test 2 Review Outline.
ECE 382 Lesson 22 Readings Lesson Outline Writing Clean Code
Chapter 7: User-Defined Functions II
Computer Science 210 Computer Organization
Computer Skills2 for Scientific Colleges
EPSII 59:006 Spring 2004.
Lecture 9: Pointers B Burlingame 25 October 2017.
Pointers.
Lecture 6 C++ Programming
User-Defined Functions
DATA HANDLING.
Computer Science 210 Computer Organization
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Call by Value Call by Reference Review
Peer Instruction 6 Java Arrays.
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
One-Dimensional Array Introduction Lesson xx
Pointers and Pointer-Based Strings
Lecture 18 Arrays and Pointer Arithmetic
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Computer Skills2 for Scientific Colleges
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Classes and Objects.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Pointers.
5.1 Introduction Pointers Powerful, but difficult to master
Chapter 7: User-Defined Functions II
Overloading functions
CS150 Introduction to Computer Science 1
Functions continued.
Suggested self-checks: Section 7.11 #1-11
Data Structures and Algorithms Introduction to Pointers
Data Structures & Algorithms
Chapter 9: Pointers and String
Pointers and dynamic objects
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
CISC181 Introduction to Computer Science Dr
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
SPL – PS2 C++ Memory Handling.
Presentation transcript:

ECE 382 Lesson 21 Readings Lesson Outline Everything You Need to Know About Pointers in C Short, Funny Pointer Video Lesson Outline Pointers Arrays Function Parameters Practice Admin Assignment 7 due BOC today Assignment 8 due BOC next Lesson KSplice pointer challenge (optional, but kind of fun)

C Language: Pointers xPtr x y Addr Value 0x200 0x201 0x202 0x203 Pointer: A pointer is a variable that holds a memory address. An address “points” to a value in memory. In assembly: mov 0(r5), r6 ; is r5 or r6 the pointer? Examples: int x = 0x1234, y= 0x5678; //assume put at 0x200 int *xPtr; int *yPtr; xPtr = &x; //what is in xPtr? y = *xPtr; //what is in y? x = y; //what is in x? *xPtr = 0x2006; //what is x? xPtr = 0x2006; *xPtr = y; //where is this stored? Token Context Description & Assignment statement Returns the address of the variable after this token * Variable declaration Variable contains the address pointing to a variable of type var_type Allows you to access the contents of the variable at which the pointer is pointing -> Structure Access a structure's elements through a structure pointer (instead of the "." notation). Also can use (*structure).element. xPtr x y Addr Value 0x200 0x201 0x202 0x203

C Language: Pointers Examples: int a = 10; // declaring an integer int * aPtr; // declaring a pointer to an integer struct point { char x, y; }; aPtr = &a; // setting the value of aPtr to the address of a *aPtr = 20; // sets a to 20 by dereferencing aPtr struct point myPoint = {1,2}; // declaring a structure of point_t, initializing // with constants struct point * myPointPtr; // declaring a pointer to a point_t myPointPtr = &myPoint; // setting the value of myPointPtr to address of // myPoint myPoint.x = 5; // sets myPoint.x to 5 (*myPointPtr).x = 10; // sets myPoint.x to 10 by dereferencing myPointPtr myPointPtr->x = 20; // sets myPoint.x 10 20 by dereferencing myPointPtr // (alternative method)

C Language: Pointers Addr Value 0x1000 0x1001 0x1002 0x1003 0x1004 Examples: unsigned char x = 0x25; // address of x is 0x1000 unsigned int y = 0x1234; // address of y is 0x1001 - 0x1002 unsigned char* xPtr = &x; // address of xPtr is 0x1003 - 0x1004 unsigned char* yPtr = &y; // address of yPtr is 0x1005 - 0x1006 Examples: // Questions are independent - reset variables to original state xPtr++; // xPtr = ? x = *xPtr + 1; // x = ? // Reset variables to original state *xPtr = 0x12; // x = ? xPtr = ? y = yPtr + *yPtr; // y = ? yPtr = ? Addr Value 0x1000 0x1001 0x1002 0x1003 0x1004 0x1005 0x1006 0x1007 xPtr yPtr x y

C Language: Arrays Addr Value 0x1000 0x1001 0x1002 0x1003 0x1004 Array: a collection of elements of the same data type stored in consecutive memory locations. Index counting starts at 0 Max index is NUM_ELEMENTS - 1 <data_type> array_name[NUM_ELEMENTS]; // Uninitialized <data_type> array_name[] = {val0, val1, ...}; // Initialized array_name decays into a pointer to the first element in the array <data_type> lets the compiler know how much to "jump" between elements in the array Examples: unsigned int a[3]; // address of a[0] is 0x1000, // address of a[1] is 0x1002, // address of a[2] is 0x1004 unsigned int* myPtr; // myPtr is 0x1006 unsigned int x; a[0] = 0x1234; a[1] = 0x5678; a[2] = 0x9ABC; x = a[0]; myPtr = a; // can I do this? x = *myPtr; x = myPtr[1]; x = *(myPtr + 2); // tricky? a = myPtr; // you can’t do that Addr Value 0x1000 0x1001 0x1002 0x1003 0x1004 0x1005 0x1006 0x1007 0x1008 0x1009

C Language: Function Parameters Pass by Value - Passing the actual value of the variable ( pass a copy ) Good choice for small-sized variables Expensive to copy larger variables (e.g. structures, arrays, etc.) Pass by Pointer - Pass pointer into variable (same as Pass by Reference) ( pass access to the original ) Constant size parameter no matter how large the object it is to point to Allows you to directly modify the variable in the function without a return statement Pass by Value Example: char x=5; y=10; z = some_function(x, y) … char some_function(char a, char b) { a += 5; b += 5; // does this change x and y? return a + b; } Pass by Reference Example: z = some_function(&x, &y) char some_function(char* a, char* b) { *a += 5; *b += 5; // does this change x and y? return *a + *b;

C Language: Practice Addr Value 0x800 0xFF 0x801 0x23 0x802 0x56 0x803 char x; // Memory location 0x0800 = 0xFF char y[3]; // Memory locations: 0x0801 = 0x23, 0x0802 = 0x56, 0x0803 = 0x89 char* letter_ptr; // Memory locations: 0x0804 - 0x0805 = 0xABDC Question: What values would be assigned using the following statements? // Questions are independent - reset variables to original state x = y[2]; // Reset variables to original state letter_ptr = y; x = *(letter_ptr + 2); letter_ptr = &x; y[2] = *(letter_ptr + 1); // x = 0x89 Addr Value 0x800 0xFF 0x801 0x23 0x802 0x56 0x803 0x89 0x804 0xDC 0x805 0xAB Addr Value 0x800 0x801 0x802 0x803 0x804 0x805 Addr Value 0x800 0xFF 0x801 0x802 0x803 0x804 0x805 Addr Value 0x800 0xFF 0x801 0x23 0x802 0x56 0x803 0x89 0x804 0x805 // letter_ptr = 0x0801 // x = 0x89 // same as x = letter_ptr[2] // letter_ptr = 0x0800 // y[2] = *(0x800 + 1) = 0x23

Writing Clean Code Okay to use i, j, k for loop counters Meaningful Names Use intention-revealing names ("self-documenting") int d, temp; // elapsed time in days, a temporary variable int elapsedTimeInDays, daysSinceModification; Make meaningful distinctions void copyChars(char* a1, char* a2) void copyChars(char* source, char* destination) Use pronounceable names DtaRcrd DataRecord Use searchable names MAX MAX_STUDENTS Functions: use verbs! forward() moveForward() Don't be cute holyHandGrenade() deleteItem() Pick one word per concept Bad: fetch(), retrieve(), get() Okay to use i, j, k for loop counters

Clean Functions Small - ideally less than 10 lines long Do one thing Use descriptive names Parameters: rarely need more than two or three Side effects - function should only do what you say it does Do not use static or global variables Only depend on local variables / parameters Don't repeat yourself - write a function instead of copy / paste Only one entry / exit point Indent correctly!

Clean Comments Comment on "big picture" items Head of each file Definition of each function Beginning of each major block of code As you move deeper in the hierarchy, the comments are more specific Try writing functions / meaningful names if ((employeeFlags & HOURLY_FLAGS) && (employeeAge > 65)) ... if (isEligibleForFullBenefits(employee)) ... TODO comments // TODO: Make this into a function // TODO: Write new header file to group these functions Bad comments Restating your code (a = 1; // Setting a to 1) Commented-Out code Too much information Don't comment bad code - rewrite it.

Assignment 8 Assignment 8 - Moving Average Write a function that computes the "moving average" for a stream of data points it is receiving. Write two other functions that compute the maximum and minimum values in a given array. You can "terminate" (last element is some predefined constant to simplify your loop) or pass in the length of the array as a parameter. Write another function that computes the range of values in an array.\ Three separate files: main.c, header, implementation