C Passing arrays to a Function

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Kernighan/Ritchie: Kelley/Pohl:
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
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.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Stack and Heap Memory Stack resident variables include:
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Week 12 Methods for passing actual parameters to formal parameters.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Department of Computer Science Western Michigan University
C++ Review Data Structures.
Two dimensional arrays.
User-Written Functions
EGR 2261 Unit 10 Two-dimensional Arrays
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
Chapter 10-1: Structure.
Multidimensional Arrays
Lecture 7 Arrays 1. Concept of arrays Array and pointers
Hassan Khosravi / Geoffrey Tien
Student Book An Introduction
Hassan Khosravi / Geoffrey Tien
JavaScript: Functions.
Java Review: Reference Types
This pointer, Dynamic memory allocation, Constructors and Destructor
C-Programming, continued
Lecture 8 – 9 Arrays with in a class
Pointers and References
14th September IIT Kanpur
Chapter 10: Pointers 1.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Object Oriented Programming COP3330 / CGS5409
Multiple Dimension Arrays
7 Arrays.
Lecture 18 Arrays and Pointer Arithmetic
prepared by Senem Kumova Metin modified by İlker Korkmaz
Topics discussed in this section:
CS2011 Introduction to Programming I Arrays (I)
Multidimensional array
Arrays Week 2.
Multidimensional Arrays
CISC181 Introduction to Computer Science Dr
7 Arrays.
Lets Play with arrays Singh Tripty
Exam 2 Exam 2 Regrading Average: 69 TA: Fardad
Submitted By : Veenu Saini Lecturer (IT)
C Programming Pointers
Chapter 9: Pointers and String
C H A P T E R F I V E Memory Management.
Structure (i.e. struct) An structure creates a user defined data type
C++ Array 1.
Pointers and References
Introduction to Pointers
Presentation transcript:

C Passing arrays to a Function Pointers with arrays Pointers with functions

Pass arrays to a function Single element of an array can be passed in similar manner as passing variable to a function.

Passing an entire one-dimensional array to a function While passing arrays as arguments to the function, only the name of the array is passed (i.e., starting address of memory area is passed as argument).

Passing Multi-dimensional Arrays to Function To pass two-dimensional array to a function as an argument, starting address of memory area reserved is passed as in one dimensional array

Pointer to an Array Declaration of array int arr[5] = { 1, 2, 3, 4, 5 }; Compiler allocates sufficient amount of memory to contain all the elements of the array. Base address: Base address i.e. address of the first element of the array (also allocated by the compiler). From the above example Base address is 1000 i.e., arr[0]

Pointer to an Array Declaration of array int arr[5] = { 1, 2, 3, 4, 5 }; arr will give the base address, which is a constant pointer pointing to the first element of the array, arr[0]. Hence arr contains the address of arr[0] i.e 1000 Printf(“address of array is =%u \n”,arr); Printf(“address of array is =%u \n”,&arr[0]);

Pointer to an Array (Examples)

Pointer to an Array (Examples)

Pointer to Multidimensional Array

Pointer to Multidimensional Array A multidimensional array is of form, a[i][j] In a[i][j], a will give the base address of this array, even a + 0 + 0 will also give the base address, that is the address of a[0][0] element.

Pointer to Multidimensional Array

Passing Pointer to a function Just like any other argument, pointers can also be passed to a function as an argument.    There are two ways to pass arguments/parameters to function calls –  call by value   call by reference.

Call by values In call by value, If you change the value of function parameter, it is changed for the current function only. It will not change the value of variable inside the caller method such as main()

Call by value (Example)

call by reference When we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value.  So actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function. This technique is known as call by reference in C.

call by reference (Example)

Difference between call by reference and call by reference (Example)

Difference between call by reference and call by reference (Example) Call by value Call by reference 1 A copy of value is passed to the function An address of value is passed to the function 2 Changes made inside the function is not reflected on other functions Changes made inside the function is reflected outside the function also 3 Actual and formal arguments will be created in different memory location Actual and formal arguments will be created in same memory location

Assignments Write a c program for Student using functions concept with following parameter Take Name and marks as arrays Create a function AvgM to calculate the average of 5 marks where were given in array Create a function displayS to display student name and average marks and their grade Write a c program for finding multiplication of two matrix using functions Write a c program for sum of array elements using pointer to array concepts Write a c program for sum two matrices using pointer to array concepts  Write a C program for swapping of two number using call by value and call by reference.