Thread: reference parameters

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    19

    reference parameters

    Due to the lack of reference parameters being available in C, i need to use pointers to change a value with a function, but I can't seem to figure out how I can pass an array to a function and change the array values, is there a thread somewhere which covers this? or can someone give me an example? thnx

    EDIT: I believe I figured it out, someone correct me if I'm wrong plz

    example:
    Code:
    #define elements 4
    
    int somearray[elements];
    
    void arraychanging(int* array[],int elements)
    {
    int counter = 0;
    
      for(counter = 0;counter<elements;counter++){
        *array[counter] = 1
      }
    }
    
    arraychanging(&somearray,elements);
    Last edited by LowLife; 01-25-2006 at 09:02 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    pointers in C are nearly identical to references in C++. There are only a couple minor differences

    1. The function that passes the argument: There is no difference in how to pass an array -- both C and C++ are identical. Passing a single object: C needs the & operator, while c++ does not
    Code:
    //
    // *.c file
    //
    struct mystruct
    {
        int a;
        int b;
    };
    
    void foo(int array[], int* b, struct mystruct* c)
    {
       c->a = 0;
       *b = 0;
    }
    
    int main()
    {
        int array[5];
        int b;
        struct mystruct c;
    
       foo(array, & b, &c);
    }
    Code:
    //
    // *.cpp
    //
    struct mystruct
    {
        int a;
        int b;
    };
    
    void foo(int array[], int& b, mystruct& c)
    {
       c.a = 0;
       b = 0;
    }
    
    
    int main()
    {
        int array[5];
        int b;
        mystruct c;
       foo(array,  b, c);
    }
    Last edited by Ancient Dragon; 01-25-2006 at 09:01 AM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    19
    Still this doesn't solve my problem, and I don't think you get what I am trying to do, I want to pass an array to the function, so I give the array address to the function with &somearray, but it seems I am doing something wrong with the function, since I always get "passing argument 1 of .... from incompatible pointer type"

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    void arraychanging(int array[], int size)
    {
       int i = 0;
       for ( i = 0; i < size; ++i )
       {
          array[i] = 1;
       }
    }
    
    int main(void)
    {
       int somearray[4];
       arraychanging(somearray, 4);
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    19
    but this will not change the array values in my main function, it will just change them within the function arraychanging, I want the array that I pass to be altered, like a reference parameter in c++, thus meaning I have to use pointers

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by LowLife
    but this will not change the array values in my main function, it will just change them within the function arraychanging, I want the array that I pass to be altered, like a reference parameter in c++, thus meaning I have to use pointers
    Have you tried it? What Dave_Sinkula posted should work. They contents of the array passed into the function should remain modified after exiting said function.

    BTW: This wouldn't work.
    Code:
    #define elements 4
    
    void arraychanging(int* array[],int elements)
    The preprocessor would turn that into this:
    Code:
    void arraychanging(int* array[],int 4)
    ...prior to compiling which would of course give you an error. That's why defines are typically made all caps to differentiate them from normal variables:
    Code:
    #define ELEMENTS  4
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    19
    Nope I tried his method just now, and it doesn't change the values of my array, can it have something to do with the fact that this function is in another file, and I am including it? though this would appear strange to me, I'm just guessing here...

    btw, i know they have to be caps, I just forgot it while wanting to be quick in my edit

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You don't normally "include" source code from one source file in another file. You would "include" a header containing a function prototype perhaps. That said, it shouldn't matter where the function's code is located. You could also try this:

    Code:
    void arraychanging(int * array, int size)
    {
        int i = 0;
        for ( i = 0; i < size; ++i )
        {
            array[i] = 1;
        }
    }
    Which is the same thing Dave posted except with a * instead of a [] in the function parameter list. That should also work.

    Why don't you try posting a complete (compilable) but minimal program example that you are trying to get working but that still demonstrates this behavior you are seeing. Make sure to delineate which code is in which source file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    19
    Hmm, sorry Dave_Sinkula, what you said was true, there seemed to be something wrong with my including files

    hk_mp5kpdw, I am using seperate source and header files, the source file are my functions, and the header file are my functions prototypes, so no problems there

    giving you my source wouldn't really help you, I'm programming in C, yes, but the app I'm creating is build for the PSP...

    anyway, I figured it out, thnx all for the help
    Last edited by LowLife; 01-25-2006 at 12:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM