how to pass array by reference

This is a discussion on how to pass array by reference within the C Programming forums, part of the General Programming Boards category; Hey guys, I have an array of 3 elements and a function that can take an array of 4 elements, ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    53

    how to pass array by reference

    Hey guys, I have an array of 3 elements and a function that can take an array of 4 elements, I want to manipulate the 4 elements in the function and pass them mack by reference, hopefully my sample code will enlighten my problem.

    Code:
    void doubleElements(int *array[3]);
    
    int main(void)
    {
        int array1[3];
        array1[0] = 2;
        array1[1] = 4;
        array1[2] = 6;
        array1[3] = 8;
    
        doubleElements(&array1);
     
    }
    
    void doubleElements(int *array[3])
    {
         array[0] = array[0]*2;
         array[1] = array[1]*2;
         array[2] = array[2]*2;
         array[3] = array[3]*2;
    }
    Is there something wrong here??? Obviously I want:
    array1[0] = 4
    array1[1] = 8
    array1[2] = 12
    array1[3] = 16

    Appreciate the help!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your array in main is under-sized - 0 to 3 IS 4 elements.

    In the call to the function, just use the name of the array, no & before it.

    Why not use a loop inside your function?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    Woops my original sentace should have been:
    "I have an array of 4 elements and a function that can take an array of 4 elements....."

    no & before it you say... Il give it a whirl thanks!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Change
    doubleElements(&array1);
    to
    doubleElements( array1, sizeof(array1) / sizeof(array1[0]) );

    Change
    void doubleElements(int *array[3])
    to
    void doubleElements(int* array, size_t size)

    And in doubleElements add:
    assert(size >= 4);

    And at top add:
    #include <assert.h>

    Finally, this passes the array by address, or pointer. There is no pass by reference in C.
    Last edited by Elysia; 09-19-2010 at 05:29 AM. Reason: Fixed bug
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Probably you meant doubleElements(array1, sizeof(array1)/ sizeof(array1[0]) );

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    True. That was my mistake. Fixed.
    Last edited by Elysia; 09-19-2010 at 06:44 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    What is the difference between doubleElements( array1, sizeof(array1) / sizeof(array1[0]) ); and doubleElements(array1, sizeof(array1)/ sizeof(array1[0]) ); ????

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What is the difference between doubleElements( array1, sizeof(array1) / sizeof(array1[0]) ); and doubleElements(array1, sizeof(array1)/ sizeof(array1[0]) ); ????
    One is on line 1, and the other is on line 2!

    Nothing. Elysia fixed it.

    Had you going there, eh?

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    k... well I've tried that code but it does not work, obviously my sample code is much different to what I am trying to write but surely the error is the same??? if I use:
    doubleElements( array1, sizeof(array1) );
    instead of what Elysia said:
    doubleElements( array1, sizeof(array1) / sizeof(array1[0]) );
    then it works but am i doing something i shouldnt? like I said the code I am writing is performing a different task (HC12 microcontroller converting 4 analogue inputs to digital ones and turning on a LED if the digital value of one channel is larger than a certain interger that I define)

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Doesn't work doesn't tell us much. You need to provide information about WHAT is going wrong and an example from your code that shows the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    OK here is pretty much my code:

    Code:
    #include <hidef.h>      //common defines and macros 
    #include "derivative.h" //derivative-specific definitions 
    #include "ADC.h"        //allows convenient use of ADC
    #include <assert.h>
    
    
    
    void DisSensors(int SampleSize, int OnThresh, int OffThresh, int* result, size_t size); 
    
    
    void main(void)
    {
      int DS[3]; //Distance Sensors
      int PS1=0, PS2=0, PS3=0, PS4=0;  //Photo sensors
      int i; 
      int average=0;
      EnableInterrupts;
      ADCInit();      //initialises the ADC before first use
      /* delay(5000); // 5 second delay before start of the round NEEDS ACCURATE CALIBRATION  */ 
    
      DDRT = (0xFF);
      PTT = (0x00);
          
      
      for(;;)
      {
           DisSensors(10, 100, 70, DS,sizeof(DS)/ sizeof(DS[0]) ); 
            
      } /* loop forever */
      /* please make sure that you never leave main */
    }
    
    
    void DisSensors(int SampleSize, int OnThresh, int OffThresh, int* result, size_t size)
    {
        int i;
        assert(size >= 4);
        result[0] = 0;
        result[1] = 0;
        result[2] = 0;
        result[3] = 0;
        for(i=1;i<=SampleSize;i++) 
        {
          ADCDoConversions(); // trigger convertion
          result[0] = result[0] + ATDDR2;
          result[1] = result[1] + ATDDR3;
          result[2] = result[2] + ATDDR4;
          result[3] = result[3] + ATDDR5;
        }
        result[0] = result[0]/SampleSize;
        result[1] = result[1]/SampleSize;
        result[2] = result[2]/SampleSize;
        result[3] = result[3]/SampleSize; 
        for(i=0;i<=0;i++)
        {
          if(result[i] >= OnThresh)
          PTT = 0x01;//result[i] = TRUE;
          else if(result[i]<=OffThresh)
          PTT = 0x00;//result[i] = FALSE;
        } 
    }

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    Doing it this way it compiles but when I go into the debugger and try run the program it doesnt run??? sorry for the length of code...

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can give DS[] another handle (name), like "result", inside your functions -- but it will still have just << THREE >> elements, if it had three elements before the called function.

    Since you are explicitly giving 3 as the size of DS, I don't see any advantage to using the division to pass the size to the function. Pass the 3 (or better yet define the SIZE as 3, (right after the include files listing, and forget passing it entirely.
    Last edited by Adak; 09-19-2010 at 09:19 AM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    See that little assert? The program should halt on that line if you're using a debug build (which you should be until you know it's bug free).
    That's because the size of the array is 3, yet the assert fails unless the size is >= 4. Which indicates you have a bug in your code, and the assert stopped your computer from blowing up due to a buffer overrun.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Pass an array to a function by reference?
    By Loic in forum C++ Programming
    Replies: 3
    Last Post: 06-05-2007, 12:44 AM
  4. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 11:37 AM
  5. How can I pass a reference from a array os a class
    By Nautilus in forum C++ Programming
    Replies: 7
    Last Post: 01-20-2003, 06:23 AM

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