Results 1 to 4 of 4

Thread: Pass Array into a function

  1. #1
    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?

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    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);  
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    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;
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    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;
    }

Similar Threads

  1. Replies: 5
    Last Post: 27-02-2010, 07:52 PM
  2. Two dimensional array to a function
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 27-11-2009, 12:47 PM
  3. How to return an array from function
    By Rilex in forum Software Development
    Replies: 3
    Last Post: 02-10-2009, 09:18 AM
  4. Pass an array as parameter via URL param
    By Cadallic in forum Technology & Internet
    Replies: 3
    Last Post: 25-07-2009, 12:15 PM
  5. Function that returns an array
    By ByteCoder in forum Software Development
    Replies: 3
    Last Post: 17-04-2009, 07:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,490,512.00777 seconds with 17 queries