0% found this document useful (0 votes)
57 views

3.0 - Passing and Returing An Array To - From A Function

This document discusses passing and returning arrays to and from functions in C programming. It explains that arrays can be passed to functions by passing their names as arguments. Within functions, arrays can be received as parameters by declaring array names or pointers. The document provides examples of calculating average of array elements and sorting an array using pointers. It also notes that entire arrays cannot be returned, but their base addresses can be returned through pointer returns. Examples return the first N even numbers as an array from a function.

Uploaded by

sangeetha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

3.0 - Passing and Returing An Array To - From A Function

This document discusses passing and returning arrays to and from functions in C programming. It explains that arrays can be passed to functions by passing their names as arguments. Within functions, arrays can be received as parameters by declaring array names or pointers. The document provides examples of calculating average of array elements and sorting an array using pointers. It also notes that entire arrays cannot be returned, but their base addresses can be returned through pointer returns. Examples return the first N even numbers as an array from a function.

Uploaded by

sangeetha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

20CST21 – Programming and Linear Data Structures

UNIT I : Pointers and Arrays, Pointers


and Strings
By
Ms.M.Sangeetha
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India

Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
Unit I : Contents
1. Pointers- Introduction
2. Pointers and 1D array
3. Passing an array to a function
4. Returning an array from function
5. Pointers and 2D array
6. NULL pointers
7. Array of pointers
8. Pointer-to-pointer
9. Generic pointers
10.Dangling Pointer
11.Using Pointers for string manipulation
12.Two dimensional array of strings
13.Array of pointers to strings.
2.0 _ Passing and Returing an array
04/28/2021 2
to_from a function
Passing an array to a function
• We can pass an individual elements or entire array to
a function as an argument.
• To pass an entire array to a function, only the name
of the array is passed as an argument.
• When address is passed as an argument, the
function definition should have an array or a pointer
as a parameter to receive the passed address.
• When an array is passed to a function, the changes
made by the function affect the original array.

2.0 _ Passing and Returing an array


04/28/2021 3
to_from a function
Function Definition with array as a parameter

• We can receive an array in a function using one of


the following two methods.

• The name of the array is passes as an argument,


which is the address of the first element of the array.
2.0 _ Passing and Returing an array
04/28/2021 4
to_from a function
Example 1 – Program to demonstrate the
average of all the elements of an array.

2.0 _ Passing and Returing an array


04/28/2021 5
to_from a function
Method 1

2.0 _ Passing and Returing an array


04/28/2021 6
to_from a function
Method 2

2.0 _ Passing and Returing an array


04/28/2021 7
to_from a function
Method 3

2.0 _ Passing and Returing an array


04/28/2021 8
to_from a function
Example 2 - Write a program in C to sort an
array using Pointer

2.0 _ Passing and Returing an array


04/28/2021 9
to_from a function
Returning an array from a function

• C does not allow to return an entire array


from a function.
• However, we can return a pointer to the base
address(address of first element of array) of
an array by specifying the name of the array
without an index.

2.0 _ Passing and Returing an array


04/28/2021 10
to_from a function
Returning an array from a function

• If we want to return a 1-D array from a


function, we have to declare a function
returning a pointer as in the following
example :

2.0 _ Passing and Returing an array


04/28/2021 11
to_from a function
Returning an array from a function

• We should not return base pointer of a local


array declared inside a function because as
soon as control returns from a function all
local variables gets destroyed.
• If we want to return a local array then we
should declare it as a static variable so that it
retains it's value after function call.

2.0 _ Passing and Returing an array


04/28/2021 12
to_from a function
Example 2 - Program to generate first N even
numbers and return as an array.

2.0 _ Passing and Returing an array


04/28/2021 13
to_from a function
2.0 _ Passing and Returing an array
04/28/2021 14
to_from a function
Thank you

2.0 _ Passing and Returing an array


04/28/2021 15
to_from a function

You might also like