Computer Science 210 Computer Organization

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

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.
Kernighan/Ritchie: Kelley/Pohl:
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Computer Science 210 Computer Organization Pointers.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Computer Science 210 Computer Organization Introduction to C.
Computer Science 210 Computer Organization Arrays.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
UNIT 5 C Pointers.
Functions and Pointers
Computer Science 210 Computer Organization
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Functions, Part 2 of 2 Topics Functions That Return a Value
C Programming Tutorial – Part I
Pointers.
Module 2 Arrays and strings – example programs.
Computer Science 210 Computer Organization
Arrays in C.
Lecture 6 C++ Programming
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Pointers.
Functions and Pointers
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Computer Science 210 Computer Organization
EKT150 : Computer Programming
Lecture 18 Arrays and Pointer Arithmetic
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays Strings and Parameter Passing CSCI N305
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Pointers.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Suggested self-checks: Section 7.11 #1-11
Arrays Arrays A few types Structures of related data items
Exercise Arrays.
Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Programming Languages and Paradigms
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Computer Science 210 Computer Organization Arrays and Pointers

The Array Structure A linear sequence of memory cells, each accessible by an an integer index, ranging from 0 to the number of cells minus 1 Like Python lists and Java array lists, C arrays support random (constant-time) access to the cells

Declaring Array Variables int integers[10]; // Cells for 10 ints char letters[5]; // Cells for 5 characters float matrix[3][2]; // A 3 by 2 matrix of floats int max = 100; // The number of cells in the next array double doubles[max]; // Cells for max doubles The syntax <element type> <variable> [<integer expression>] declares an array variable and reserves memory for a definite number of cells (use more [] for more dimensions), each of which can store an element of the given type (arrays are statically typed) Each cell initially contains garbage!

Initializing and Processing int i, max = 10, array[max]; // Declare variables for (i = 0; i < max; i++) // Store 1..max in the array array[i] = i + 1; for (i = 0; i < max; i++) // Print the contents printf("%d\n", array[i]); Here we initialize all of the array’s cells and visit them all The loop is pretty standard

Initializing Arrays that Are Not Full int number, length = 0, max = 10, array[max]; printf("Enter a number or 0 to stop: "); scanf("%d", &number); while (number){ array[length] = number; length++; if (length == max){ printf("Maximum number of numbers entered\n"); break; } The number of data values is tracked with a separate variable This length variable is also used in processing the array later

Processing Arrays that Are Not Full int number, length = 0, max = 10, array[max]; // Get the array’s data and length here // Print all of the currently available values int i; for (i = 0; i < length; i++) printf("%d\n", array[i]); The number of data values is tracked with a separate variable This length variable is also used in processing the array later

Arrays and Functions #include <stdio.h> #include "numbers.h" // Use a library of array processing functions int main(){ int max = 100; int numbers[max]; int length = getNumbers(numbers, max); if (length > 0) printf("The average is %f\n", sum(numbers, length) / (double) length); else printf("No numbers were entered\n"); } An array can be passed as an argument to a function The function can access or replace values in the array cells

Example: Find the Minimum // Returns the index of the minimum value in the array int minIndex(int array[], int length){ int probe, minPos = 0; for (probe = 1; probe < length; probe++) if (array[probe] < array[minPos]) minPos = probe; return minPos; } The element type of the array parameter must be specified, but its physical size need not be Therefore, this function can process an integer array of any physical size The logical size (length) is passed too, because the array might not be full

How Parameters Are Passed to Functions Parameters of basic types (char, int, float, and double) are passed by value (a copy of the value of the argument is placed in temporary storage on the runtime stack) A copy of an array’s base address is also placed in temporary storage, so its cells can still be accessed or modified

The Pointer C allows the programmer to access the address of a memory location and use it as a data value Pointers are also used to refer to dynamically allocated storage (from the system heap)

The Address of Operator & int number = 10; printf("The value is %d\n", number); // The contents of number printf("The address is %p\n", &number); // The address of number The & operator returns the actual memory address (a large integer, probably) of a variable The printf format flag p is used to print an address

Declaring a Pointer Variable int number = 10; int *alias; The * operator says that the variable alias can contain a pointer to a memory location that can hold an int number alias The memory for alias contains garbage until it is set to a value 10

Assigning an Address int number = 10; int *alias; int aCopy = number; // Copies number’s value to aCopy alias = &number; // Copies number’s address to alias The variables number and aCopy name separate storage locations The variables number and alias can access the same storage location aCopy number alias The memory for alias now contains the address of number 10 10

Access by Indirection (Dereferencing) int number = 10; int *alias = NULL; // Set the pointer variable to empty int aCopy = number; // Copies number’s value to aCopy alias = &number; // Copies number’s address to alias printf("The value is %d\n", number); printf("The value is %d\n", *alias); // Dereference to get value The * operator is also the dereference operator Its operand must be a pointer variable, and it must contain the address of another cell in memory The value NULL from stdio is used to indicate the empty pointer

Aliasing and Side Effects int number = 10; int *alias = NULL; // Set the pointer variable to empty int aCopy = number; // Copies number’s value to aCopy alias = &number; // Copies number’s address to alias (*alias) = 69; // Reset the storage by indirection printf("The value is %d\n", number); // Prints 69 * Has a lower precedence than = *alias and number access the same storage aCopy number alias Ouch!!! 10 69

Input and Output Parameters Parameters are passed by value, so they’re good as input-only parameters Pointers can be used to implement pass by reference, so they’re good for output parameters (in functions that return multiple values) Example: define a function that returns two quadratic roots, given the inputs a, b, and c

Use & with the Actual Parameter #include <stdio.h> #include <math.h> int main(){ float a, b, c, root1, root2; printf("Enter a, b, and c: "); scanf("%f%f%f", &a, &b, &c); quadraticRoots(a, b, c, &root1, &root2); printf("Root1: %f\nRoot2: %f\n", root1, root2); } The function quadraticRoots has three input parameters and two output parameters The address of a variable is passed for each output parameter The function scanf also uses this mechanism

Use * with the Formal Parameter #include <stdio.h> #include <math.h> void quadraticRoots(float a, float b, float c, float *root1, float *root2); int main(){ float a, b, c, root1, root2; printf("Enter a, b, and c: "); scanf("%f%f%f", &a, &b, &c); quadraticRoots(a, b, c, &root1, &root2); printf("Root1: %f\nRoot2: %f\n", root1, root2); } Input parameters Output parameters In the function header, the root1 and root2 parameters are of type pointer (*) to float This allows them to receive the addresses of float variables when the function is called

Use * to Access or Modify Data #include <stdio.h> #include <math.h> void quadraticRoots(float a, float b, float c, float *root1, float *root2); int main(){ float a, b, c, root1, root2; printf("Enter a, b, and c: "); scanf("%f%f%f", &a, &b, &c); quadraticRoots(a, b, c, &root1, &root2); printf("Root1: %f\nRoot2: %f\n", root1, root2); } float *root1, float *root2){ (*root1) = - b + sqrt(b * b - 4 * a * c) / (2 * a); (*root2) = - b - sqrt(b * b - 4 * a * c) / (2 * a); Input parameters Output parameters Use dereference (*) to store an integer in the implementation

Pointers and Arrays An array variable is just a name for the base address of the array storage in memory Normally, an array variable is not an l-value, meaning that it cannot be the target of an assignment statement But, this address can be copied to another variable, which can then be modified

Process an Array Using [] #include <stdio.h> int main(){ int i, max = 5, array[max]; for (i = 0; i < max; i++) array[i] = i + 1; // Print ‘em using the array and the subscript operator printf("%d\n", array[i]); }

Process the Array Using * #include <stdio.h> int main(){ int i, max = 5, array[max]; for (i = 0; i < max; i++) array[i] = i + 1; // Print ‘em using a pointer and dereference int *arrayPtr, *lastAddress = array + max - 1; for (arrayPtr = array; arrayPtr <= lastAddress; arrayPtr++) printf("%d\n", *arrayPtr); arrayPtr is an alias for array (the address of the first cell) lastAddress is the address of the last cell in the array structure arrayPtr is incremented to move through the array structure, and * is used to access each cell

For Friday Strings in C