Question with Arrays and Functions

This is a discussion on Question with Arrays and Functions within the C Programming forums, part of the General Programming Boards category; Just a general question. If I have a function(called Function 1). Inside this function I call another function (Function 2, ...

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

    Question with Arrays and Functions

    Just a general question. If I have a function(called Function 1). Inside this function I call another function (Function 2, has no parameters). Within Function 2, I create an array and will need to use this array in Function 1. Do I need to return the array(more difficult in C) or can I somehow access the array from Function 1?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,621
    You can create the array in Function 1 instead, then pass a pointer to the first element of this array to Function 2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    Quote Originally Posted by laserlight View Post
    You can create the array in Function 1 instead, then pass a pointer to the first element of this array to Function 2.
    Can't create the array in Function 1. A small sample of what I am doing.

    Code:
    //header
    struct Data{
      
      int size;  
      double meas[50];
     };
     
     struct Data cont;
    Code:
    int Test (....)
    {
    //Call Function
    StartTest();
    ....
    //use cont.meas[x] array
    return 0;
    }
    Code:
    double StartTest(void)
    ....
    ....
    //Here an array will be created as: cont.meas and want to return this array
    I won't be able to test for few more days, just trying to get it correct for now.

    Thought about something like this to return array from StartTest
    Code:
    double *StartTest(void)
    {
     double *meas = malloc(50*sizeof(double));
    
     //create cont.meas array
     return cont.meas;
    }
    Last edited by PW.EE; 09-05-2012 at 11:00 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,621
    I suspect that you actually want:
    Code:
    struct Data {
        int size; 
        double *meas;
    };
    This way, you can write:
    Code:
    void Data_init(struct Data *data, int size)
    {
        data->size = size;
        data->meas = malloc(size * sizeof(data->meas[0]));
    }
    
    void Data_destroy(struct Data *data)
    {
        data->size = 0;
        free(data->meas);
        data->meas = NULL;
    }
    
    struct Data StartTest(void)
    {
        struct Data data;
        Data_init(&data, 50);
        // Populate array through data.meas
        // ...
        return data;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    That might be. I will give it a shot and report back. Thanks

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    I am using software with predefined functions and the function creates an array and that array I am trying to return.

    I was wondering if this logic will return the array to be able to use in RunTest?

    header file
    Code:
    struct Data{
         double mData[50];
         int size;
     };
    
     struct Data cMeas;
    c file
    Code:
    int RunTest(x,y,z...)
    {
        double *mData;
        int size;
    
                
                //Run Test
                mData = StartTest();
                
                size = sizeof mData / sizeof mData[0];
    
                for (int index = 1; index < cMeas.size; index++)
                {
                   ..........
                  ...........
                  ............ cMeas.mData[index-1];
                }
    
         return 0;
    }
    
    double *StartTest(void)
    {
         .......
         .......
         double *mData = malloc(50*sizeof(double));
    
         ......
         ......
         functionReturnsArray(x,y,cMeas.mData,z); //functions takes in values and stores in array
    
         return cMeas.mData;
    }
    Last edited by PW.EE; 09-09-2012 at 03:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and functions? (easy question)
    By Lej1 in forum C Programming
    Replies: 5
    Last Post: 03-10-2012, 04:14 AM
  2. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  3. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  4. Arrays and Functions
    By WellyJ in forum C Programming
    Replies: 6
    Last Post: 03-19-2003, 08:38 AM
  5. 2-d arrays and functions
    By Faceoff49 in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2002, 08:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21