Thread: Function: passing 2-D array and returning a 1-D array from a function

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    13

    Function: passing 2-D array and returning a 1-D array from a function

    Code:
    main()
    {
         int a[10][5],j,i;
    for(j=0;j<5;j++)
         for(i=0;i<10;i++)
                  scanf("%d",&a[i][j]);
            
    prior(a,j);
    return 0;
    }
    int prior(int a[][],int j)
     {
        int m=-1,p,k,c[100];
        for(p=0;p<j;p++)
        for(k=0;k<3*n;k+3)
        {
            m=m+1;
            c[m]=a[k][i]-(a[k+1][i]+a[k+2][i]);
        }
    return c[];
    Please correct my code to work....

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The easiest thing to do is pass in the array where you want the results stored.
    Eg.
    Code:
    void prior( int a[][5], int c[], int j);
    int main ( ) {
        int a[10][5], c[100];
        prior(a,c,j);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    13

    Re:Function: passing 2-D array and returning a 1-D array from a function

    Quote Originally Posted by Salem View Post
    The easiest thing to do is pass in the array where you want the results stored.
    Eg.
    Code:
    void prior( int a[][5], int c[], int j);
    int main ( ) {
        int a[10][5], c[100];
        prior(a,c,j);
    }
    How can I return a 1-D array c[100]?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You just do things like
    c[0] = 0;
    inside your function, and that will make the change in the array declared in main.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, you haven't declared n or i and there is a missing '}' in your prior function ( the '}' is more likely a "copy and paste" problem)
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning an array from a function and using again in function
    By shrinivasapte in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2011, 10:27 AM
  2. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Returning an array from a function
    By cjschw in forum C++ Programming
    Replies: 10
    Last Post: 07-20-2003, 12:03 AM
  5. returning an array from a function
    By BubbleBoy in forum C Programming
    Replies: 1
    Last Post: 02-20-2003, 12:41 PM

Tags for this Thread