Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Passing Arrays to Functions in C Programming

August 25, 2013 Posted by: GPA Views: 565 0 comments

Problem

Pass array to method and write simple application.

Solution

In some case we need to pass array to function. In this example you will pass array type integer to method "multiplyeacharrayelementby2" of type void which will take an array of type integer and size of type integer as its input, it will multiply the array elements by 2:

void multiplyeacharrayelementby2(int inputarray[],int size);

Example:

 

#include <stdio.h>
#include <stdlib.h>
void multiplyeacharrayelementby2(int inputarray[],int size);
int main(int argc, char *argv[])
{
   int inputarray[]={5,2,6,4,65,5};
   int size=6;
   int i;
   printf("Passing array by reference:\n\n");
   printf("The values of the original array are::\n");
   for ( i = 0; i < size; i++) {
        printf("%d ",inputarray[i]);
   }
   //call method multiplyeacharrayelementby2
   multiplyeacharrayelementby2(inputarray,size);
   //show new result
   printf("\nThe values of the array after multiply are:\n");
   for ( i = 0; i < size; i++ ) {
        printf("%d ",inputarray[i]);
   }
   printf("\n");
   system("PAUSE");
   return 0;
}
void multiplyeacharrayelementby2(int inputarray[],int size){
       int i; /* counter */
        //multiply each array element by 2
        for ( i = 0; i < size; i++ ) {
            inputarray[i] *= 2;
     } /* end for */
}

 

Output

Passing Arrays to Functions in C

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe