Array as arguments, Multidimensional arrays

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

Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
C arrays (Reek, Ch. 8) 1CS 3090: Safety Critical Programming in 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.
POINTERS.
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.
Engineering Computing I Chapter 5 Pointers and Arrays.
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.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Windows Programming Lecture 03. Pointers and Arrays.
Lecture 5 Pointers 1. Variable, memory location, address, value
C arrays (Reek, Ch. 8) CS 3090: Safety Critical Programming in C.
Data Types In Text: Chapter 6.
Computer Science 210 Computer Organization
CSE 374 Programming Concepts & Tools
Standard Version of Starting Out with C++, 4th Edition
Pointers & Arrays.
Multidimensional Arrays
CNG 140 C Programming (Lecture set 10)
2-D arrays a00 a01 a02 a10 a11 a12 a20 a21 a22 a30 a31 a32
Java Review: Reference Types
Arrays in C.
Pointer.
Lecture 6 C++ Programming
Pointers and Arrays Chapter 12
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Computer Science 210 Computer Organization
C Passing arrays to a Function
Chapter 5 POINTERs.
Multiple Dimension Arrays
7 Arrays.
Lecture 10 Arrays.
EKT150 : Computer Programming
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Constructors and destructors
Pointers and Arrays Chapter 12
Multidimensional Arrays
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Regrading Project 2 Exam 2 One week period TA: Huiyuan TA: Fardad
Pointers and Arrays Chapter 12
2-d Arrays.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
MSIS 655 Advanced Business Applications Programming
Homework Starting K&R Chapter 5 Good tutorial on pointers
Introduction to Computing Lecture 10 Arrays (Part 1)
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Dr Tripty Singh Arrays.
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.
Pointers & Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
Standard Version of Starting Out with C++, 4th Edition
Chapter 5 POINTERs Visit to more Learning Resources.
ENERGY 211 / CME 211 Lecture 11 October 15, 2008.
Introduction to Pointers
Presentation transcript:

Array as arguments, Multidimensional arrays CS 3090: Safety Critical Programming in C

Review of arrays 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); … } 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 as a function argument Ret_type fun (int array[length]) { …. } int main() int N=10; int A[N]; fun(A); Ret_type fun (int array[],int length) { …. } int main() int N=10; int A[N]; fun(A,N); Method 1 Method 2 CS 3090: Safety Critical Programming in C

Example #include <stdio.h> float print_array(int x[],int N) { int i; for (i=0;i<N;i++) printf("%d\n",x[i]); } void fill_array(int x[],int N) for(i=0;i<N;i++) x[i]=i+1; int main() int N=10; int x[N]; fill_array(x,N); print_array(x,N);

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)+2) *(d+1) Then increment by the size of 2 ints 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}; 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

int X[N][N],Y[N][N],Res[N][N],i,j,k; printf("enter X>>\n"); int main() { int X[N][N],Y[N][N],Res[N][N],i,j,k; printf("enter X>>\n"); initial_matrix(N,X); printf("enter Y>>\n"); initial_matrix(N,Y); //multiply the two matrix: for (i=0;i<N;i++) { for (j=0;j<N;j++) Res[i][j]=0; for (k=0;k<N;k++) Res[i][j]+=X[i][k]*Y[k][j]; } //print the result: print_matrix(Res); Matrix multiplication CS 3090: Safety Critical Programming in C

void initial_matrix(int N,int X[][N]) { int i,j; for (i=0;i<N;i++) for (j=0;j<N;j++) scanf("%d",&X[i][j]); } void print_matrix(int mat[][N]) { int i,j; for(i=0;i<N;i++) for (j=0;j<N;j++) printf("%3d",mat[i][j]); } printf("\n");