ECE 103 Engineering Programming Chapter 32 Array Parameters

Slides:



Advertisements
Similar presentations
ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
Advertisements

1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
ECE 103 Engineering Programming Chapter 9 gcc Compiler Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
ECE 103 Engineering Programming Chapter 45 Pointers to Functions Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Chapter 7: User-Defined Functions II
Computer Science 210 Computer Organization
CSE 220 – C Programming Pointers.
Functions and Pointers
Hassan Khosravi / Geoffrey Tien
Pointers and Pointer-Based Strings
Pointers.
INC 161 , CPE 100 Computer Programming
Command-Line Arguments
Functions and Pointers
Computer Science 210 Computer Organization
Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.
Multidimensional Arrays
C Arrays.
7 Arrays.
Dale Roberts, Lecturer IUPUI
Arrays Strings and Parameter Passing CSCI N305
Pointers Call-by-Reference CSCI 230
Outline Defining and using Pointers Operations on pointers
Pointers (continued) Chapter 11
MSIS 655 Advanced Business Applications Programming
Simulating Reference Parameters in C
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
7 Arrays.
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 37 C Macro Parameters
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
ECE 103 Engineering Programming Chapter 62 Stack Implementation
ECE 103 Engineering Programming Chapter 64 Tree Implementation
Pointers and Pointer-Based Strings
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
Arrays Arrays A few types Structures of related data items
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
ECE 103 Engineering Programming Chapter 18 Iteration
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
ECE 103 Engineering Programming Chapter 22 Selection
C Parameter Passing.
Presentation transcript:

ECE 103 Engineering Programming Chapter 32 Array Parameters Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

Syllabus 1D Array Parameters Multi-D Array Parameters Examples

Passing Arrays to Functions (1-D) Parameter list declaration: dtype fun_name (dtype array_name[size]) { /* Function body */ } In the parameter list, specifying the 1-D array’s size is optional (the brackets are still required). When passing an array to a function, use just the array name as the argument to the function. Do not pass the brackets! (pass M, not M[]) 2

Example: #include <stdio.h> #define SIZE 3 int myfun (int c, int P[SIZE], int Q[]); int main (void) { int M[SIZE] = {12, 32, -5}; int Q[] = {1, 3, 4}; printf("%d\n", myfun(127, M, Q)); return 0; } int myfun (int c, int P[SIZE], int Q[]) { return P[0]*Q[2] + P[1]*Q[1] + P[2]*Q[0] + c; } 3

Only the memory address of the array is passed to the function. Pass-by-value: A passed array address that is stored in a function parameter is only a local copy. The address value cannot be permanently altered. However, the contents of the array at that address can be permanently changed! If the values of a passed array should never be changeable, then use a const modifier. If a function needs to know the number of elements in a passed array, that number must also be passed as a separate argument. 4

Example: #include <stdio.h> #define SIZE 100 void printarray (int P[], int NumValidElements); int main (void) { int M[SIZE], value, k = 0; while (k < SIZE) { scanf("%d", &value); if (value < 0) break; M[k++] = value; } printarray(M, k); return 0; } void printarray (int P[], int NumValidElements) { int k; for (k = 0; k < NumValidElements; k++) printf("%d ", P[k]); printf("\n"); 5

Example: #include <stdio.h> #define SIZE 10 /* Declare P to be a normal changeable array */ /* Declare Q to be a constant unchangeable array */ void fun (int P[], const int Q[], int ArySize); int main (void) { int M[SIZE] = {0}, N[SIZE] = {0}; fun(M, N, SIZE); return 0; } void fun (int P[], const int Q[], int ArySize) { P[0] = 99; /* This is allowed */ Q[0] = 99; /* ERROR: Compiler will complain */ } 6

Example: Actual output: y=99 : x=1 2 y=99 : x=1024 4096 #include <stdio.h> void fun(int y, int x[]) { y = 42; x[0] = 1024; x[1] = 4096; } int main (void) int y = 99; int x[] = {1, 2}; printf("y=%d : x=%d %d\n", y, x[0], x[1]); fun(y, x); return 0; Actual output: y=99 : x=1 2 y=99 : x=1024 4096 7

Passing Arrays to Functions (Multi-Dimensional) Parameter list declaration: dtype fun_name (dtype array_name[size1][size2]…[sizeN]) { /* Function body */ } Specifying size1 is optional (the brackets are still required). The remaining sizes must be specified. When passing an array to a function, use just the array name as the argument to the function. Do not pass the brackets! (pass M, not M[][]) 8

Example: #include <stdio.h> void myfun (int P[7][3][2], int Q[][2][3]); int main (void) { int M[7][3][2] = {{{0}}}; int N[2][2][3] = {{{0}}}; myfun(M, N); return 0; } void myfun (int P[7][3][2], int Q[][2][3]) /* Function body */ 9