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

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Programming and Data Structure
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.
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.
C arrays (Reek, Ch. 8) 1CS 3090: Safety Critical Programming in C.
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.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
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.
POINTERS.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
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.
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.
Engineering Computing I Chapter 5 Pointers and Arrays.
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.
Windows Programming Lecture 03. Pointers and Arrays.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Dynamic Allocation in C
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Standard Version of Starting Out with C++, 4th Edition
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Motivation and Overview
Pointers and Memory Overview
CNG 140 C Programming (Lecture set 10)
Instructor: Ioannis A. Vetsikas
Lecture 6 C++ Programming
Introduction to Pointers
Introduction to Pointers
14th September IIT Kanpur
Chapter 10: Pointers 1.
7 Arrays.
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Array as arguments, Multidimensional arrays
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
MSIS 655 Advanced Business Applications Programming
Pointers Lecture 2 Tue, Jan 24, 2006.
Chapter 16 Pointers and Arrays
Homework Starting K&R Chapter 5 Good tutorial on pointers
7 Arrays.
C++ Pointers and Strings
Pointers Pointers point to memory locations
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
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.
Arrays Arrays A few types Structures of related data items
Data Structures and Algorithms Introduction to Pointers
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
Programming in C Pointers and Arrays.
Standard Version of Starting Out with C++, 4th Edition
CS31 Discussion 1H Fall18: week 6
Introduction to Pointers
C++ Pointers and Strings
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Presentation transcript:

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

Review of arrays 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 []) ??? (int) ??? (int) ??? (int) ??? (int) m CS 3090: Safety Critical Programming in C

Subscripts and pointer arithmetic 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] CS 3090: Safety Critical Programming in C

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

Array names as function arguments 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) 17 (int) 42 (int) 17 (int) (int []) a b x y g f CS 3090: Safety Critical Programming in C

Array names as function arguments 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 CS 3090: Safety Critical Programming in C

Array names as function arguments 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 []) (char []) d (char) o (char) g (char) NUL (char) string original (char []) (char []) d (char) ??? (char) o (char) ??? (char) g (char) ??? (char) ??? (char) NUL (char) buffer copy strcpy f CS 3090: Safety Critical Programming in C

When can array size be omitted? 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 CS 3090: Safety Critical Programming in C

Multidimensional arrays 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) (int) (int) (int) (int) (int) (int) (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] CS 3090: Safety Critical Programming in C

Subscripting in a multidimensional array int d[2][4]; d [1] [2] *(d+1) Then increment by the size of 2 ints *(*(d+1)+2) Increment by the size of 1 array of 4 ints (int) (int) (int) (int) (int) (int) (int) (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] CS 3090: Safety Critical Programming in C

Why do we care about storage order? 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}; 1 2 3 4 5 6 7 CS 3090: Safety Critical Programming in C

Multidimensional arrays as parameters 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 CS 3090: Safety Critical Programming in C