passing an array to function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravichobey
    New Member
    • Mar 2008
    • 6

    passing an array to function

    Hi,

    I am reading an 256 words of an hard disk drive and storing it in an array 256 volatile shorts and now i want to write the same 256 words starting from 0 to the fourth sector.

    i want to know how to pass this array to a function.

    Regards,
    Ravi
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    Your question is a bit vague. If you don't know how to pass an array to a function, then here is an example of a function which takes an array of shorts and prints it to the screen:
    [code=cpp]
    void printarray(shor t * a, int a_size)
    {
    for(int i = 0; i < a_size; ++i)
    {
    // printf("arr[%i]=%g\n",i,a[i]); // in C
    std::cout << "arr[" << i << "]=" << a[i] << std::endl; // in C++
    }
    }

    int main()
    {
    const int arr_size= 5;
    short arr[arr_size] = {35, -14, 98, 54, 1};
    printarray(arr, arr_size);
    }
    [/code]
    In C++, you should use a reference to the array so the argument list of printarray should be: (short * & a, int& a_size).
    Since printarray doesn't change the array, it sould use const keywords to make sure no changes happen accidentally (and this could also lead to some compiler optimizations): (const short * const& a, const int & a_size)

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Passing arrays to functions is covered in this article: http://bytes.com/forum/thread772412.html.

      Since your array is volatile, you may want to pass a copy to your function to avoid race conditions where the array changes while your function is in progress.

      Comment

      • ravichobey
        New Member
        • Mar 2008
        • 6

        #4
        Hi Arnaudak and weaknessforcats ,

        I know how to pass an array to a function.Still thanks for suggestions.I am using C language for my project.What i am doing:

        read_function()
        {
        reading an array of 255 volatile short variables in a[255]. and passing this array to write_function( ).
        }
        write_function( )
        {
        Passing that array so that,my first variable write at the first byte of fourth sector.
        }
        My qus, how i will retain the values in my array between function calls?

        Even i have tried to declare it as static,but i am not getting the desired output.

        Regards,
        Ravi

        Comment

        • arnaudk
          Contributor
          • Sep 2007
          • 425

          #5
          I think I still don't understand your question. If your function calls mess with your array, can't you copy it element by element to another temporary array and restore it later? If you declared it as static, it will retain its value between function calls but it seems that you overwrite this value with read_function.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by ravichobey
            read_function()
            {
            reading an array of 255 volatile short variables in a[255]. and passing this array to write_function( ).
            }
            write_function( )
            {
            Passing that array so that,my first variable write at the first byte of fourth sector.
            }
            My qus, how i will retain the values in my array between function calls?
            Post your code. This outline shows you are not passing anything to these functions.

            Also, a word is usually either 32 or 64 bits while a short is 16. Further, a short is signed so it's really 15. But maybe you are using some operating system that has a 16 bit word. In any case, the array should be unsigned.

            Comment

            Working...