C arrays (Reek, Ch. 8) 1CS 3090: Safety Critical Programming in C.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Programming and Data Structure
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.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
C strings (Reek, Ch. 9) 1CS 3090: Safety Critical Programming in C.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
0 Chap. 5 Pointers and Arrays 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
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”
POINTERS.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Pointers *, &, array similarities, functions, sizeof.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Duke CPS 6. 1 arrays and strings: pointers and memory allocation l Why not rely solely on string and Vector classes? ä how are string and Vector implemented?
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Arrays and Pointers.
Engineering Computing I Chapter 5 Pointers and Arrays.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
Today’s Material Strings Definition Representation Initialization
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
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.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Chapter 12: Pointers and Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Windows Programming Lecture 03. Pointers and Arrays.
C arrays (Reek, Ch. 8) CS 3090: Safety Critical Programming in C.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Lecture 6 C++ Programming
7 Arrays.
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Array as arguments, Multidimensional arrays
Chapter 16 Pointers and Arrays
Homework Starting K&R Chapter 5 Good tutorial on pointers
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Data Structures and Algorithms Introduction to Pointers
SPL – PS2 C++ Memory Handling.
Presentation transcript:

C arrays (Reek, Ch. 8) 1CS 3090: Safety Critical Programming in C

Review of arrays CS 3090: Safety Critical Programming in C2  There are no array variables in C – only array names  Each name refers to a constant pointer  Space for array elements is allocated at declaration time  Can’t change where the array name refers to…  but you can change the array elements, via pointer arithmetic int m[4]; (int []) m ??? (int) ??? (int) ??? (int) ??? (int)

Subscripts and pointer arithmetic CS 3090: Safety Critical Programming in C3  array[subscript] equivalent to *(array + (subscript))  Strange but true: Given earlier declaration of m, the expression 2[m] is legal!  Not only that: it’s equivalent to *(2+m) *(m+2) m[2]

Array names and pointer variables, playing together CS 3090: Safety Critical Programming in C4 int m[3]; int *mid = m + 1; int *right = mid[1]; int *left = mid[-1]; int *beyond = mid[2]; (int []) beyond ??? (int) ??? (int) ??? (int) (int []) mid right left m subscript OK with pointer variable compiler may not catch this – runtime environment certainly won’t

 In C, arguments are passed “by value”  A temporary copy of each argument is created, solely for use within the function call void f(int x, int *y) { … } void g(…) { int a = 17, b = 42; f(a, &b); … }  Pass-by-value is “safe” in that the function plays only in its “sandbox” of temporary variables –  can’t alter the values of variables in the callee (except via the return value) Array names as function arguments CS 3090: Safety Critical Programming in C5 17 (int) 42 (int) g b 17 (int) xy (int []) f a

Array names as function arguments CS 3090: Safety Critical Programming in C6  But, functions that take arrays as arguments can exhibit what looks like “pass-by-reference” behavior, where the array passed in by the callee does get changed  Remember the special status of arrays in C – They are basically just pointers.  So arrays are indeed passed by value – but only the pointer is copied, not the array elements!  Note the advantage in efficiency (avoids a lot of copying)  But – the pointer copy points to the same elements as the callee’s array  These elements can easily be modified via pointer manipulation

Array names as function arguments CS 3090: Safety Critical Programming in C7  The strcpy “string copy” function puts this “pseudo” call- by-reference behavior to good use void strcpy(char *buffer, char const *string); void f(…) { char original[4] = ″ dog ″ ; char copy[4]; strcpy(copy, original); } (char []) original d (char) o (char) g (char) NUL (char) (char []) copy ??? (char) ??? (char) ??? (char) ??? (char) f (char []) string (char []) buffer strcpy d (char) o (char) g (char) NUL (char)

When can array size be omitted? CS 3090: Safety Critical Programming in C8  There are a couple of contexts in which an array declaration need not have a size specified:  Parameter declaration: int strlen(char string[]);  As we’ve seen, the elements of the array argument are not copied, so the function doesn’t need to know how many elements there are.  Array initialization: int vector[] = {1, 2, 3, 4, 5};  In this case, just enough space is allocated to fit all (five) elements of the initializer list

Multidimensional arrays CS 3090: Safety Critical Programming in C9  How to interpret a declaration like: int d[2][4];  This is an array with two elements:  Each element is an array of four int values  The elements are laid out sequentially in memory, just like a one-dimensional array  Row-major order: the elements of the rightmost subscript are stored contiguously (int) d[0][0] d[0][1]d[0][2]d[0][3]d[1][0]d[1][1]d[1][2]d[1][3] d[0] d[1]

i nt d[2][4]; d [1] [2] Subscripting in a multidimensional array CS 3090: Safety Critical Programming in C10 (int) d[0][0] d[0][1]d[0][2]d[0][3]d[1][0]d[1][1]d[1][2]d[1][3] d[0] d[1] *(d+1) *(*(d+1)+2) Increment by the size of 1 array of 4 int s Then increment by the size of 2 int s

Why do we care about storage order? CS 3090: Safety Critical Programming in C11  If you keep within the “paradigm” of the multidimensional array, the order doesn’t matter…  But if you use tricks with pointer arithmetic, it matters a lot  It also matters for initialization  To initialize d like this:  use this: int d[2][4] = {0, 1, 2, 3, 4, 5, 6, 7};  rather than this int d[2][4] = {0, 4, 1, 5, 2, 6, 3, 7};

Multidimensional arrays as parameters CS 3090: Safety Critical Programming in C12  Only the first subscript may be left unspecified void f(int matrix[][10]);/* OK */ void g(int (*matrix)[10]);/* OK */ void h(int matrix[][]);/* not OK */  Why?  Because the other sizes are needed for scaling when evaluating subscript expressions (see slide 10)  This points out an important drawback to C: Arrays do not carry information about their own sizes! If array size is needed, you must supply it somehow (e.g., when passing an array argument, you often have to pass an additional “array size” argument) – bummer