Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 24-12-2009
Member
 
Join Date: Jul 2006
Posts: 289
Pass Array into a function

After making some tests on the functions, i got some problems. First, I declared an array of the form array [0] to [5] and had sent it to function Since the pointer can not be changed to array [] []. Can any one tell me how pass array into a function?
Reply With Quote
  #2  
Old 24-12-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Pass Array into a function

Array of function pointer :
Code:
#include <stdio.h>

void printarr(int b[]) {
    int j;
    for(j = 0;j<5;j++) {
        printf(" %d\n",b[j]);
    }
}

main() {
    int b[5];
    int j;
    
    for(j = 0;j<5;j++) {
        a[j]=j; 
    }
    printarr(b);  
}
Reply With Quote
  #3  
Old 24-12-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Pass Array into a function

Code:
float sum(float b[], int m) {
  float sum = 0.0;
  intj;
  for (j = 0; j< n; j++) {
    sum += b[j];
  }
  return sum;
}
Reply With Quote
  #4  
Old 24-12-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: Pass Array into a function

Code:
#include <stdio.h>

int sum(int d, int f);
int subtract(int d, int f);
int mul(int d, int f);
int div(int d, int f);

int (*r[4]) (int q, int e);

int main(void)
{
  int result;
  int k, l, op;

  r[0] = sum; /* address of sum() */
  r[1] = subtract; /* address of subtract() */
  r[2] = mul; /* address of mul() */
  r[3] = div; /* address of div() */

  printf("Enter two numbers: ");
  scanf("%d %d", &k, &l);
  
  printf("0: Add, 1: Subtract, 2: Multiply, 3: Divide\n");
  do {
    printf("Enter number of operation: ");
    scanf("%d", &op);
  } while(op<0 || op>3);

  result = (*p[op]) (k, l);
  printf("%d", result);

  return 0;
}

int sum(int d, int f)
{
  return d + f;
}

int subtract(int d, int f)
{
  return d - f;
}

int mul(int d, int f)
{
  return d * f;
}

int div(int d, int f)
{
  if(t) 
      return d / f;
  else 
      return 0;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Pass Array into a function"
Thread Thread Starter Forum Replies Last Post
How to pass function with parameters to another function in PHP? Linoo Software Development 5 27-02-2010 07:52 PM
Two dimensional array to a function Chrisch Software Development 3 27-11-2009 12:47 PM
How to return an array from function Rilex Software Development 3 02-10-2009 09:18 AM
Pass an array as parameter via URL param Cadallic Technology & Internet 3 25-07-2009 12:15 PM
Function that returns an array ByteCoder Software Development 3 17-04-2009 07:02 PM


All times are GMT +5.5. The time now is 12:15 AM.