Thread: loading parameters into a function

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    loading parameters into a function

    Hello!
    I have a question to make. As i see if i want to load a byte in a function the code is

    void name(uchar dbyte) {
    ...
    }

    But, i want to pass 2 integers (x,y coordinations) and an array into the function.
    How can i do it? And how can i call then the function?

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Code:
    void name(uchar dbyte, uchar dbyte2) {
    call the function like this:
    Code:
    int main(void)
    {
        uchar dbyte = value_here;
        uchae dbyte2 = value_here;
        .....
        name(dbyte,dbyte2);
        .....
        return 0;
    }

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    void Function(int X, int Y, int* Array)
    {
       ...
    }
    
    ...
    
    int X;
    int Y;
    int Array[123];
    
    Function(X, Y, Array);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Magos
    Code:
    void Function(int X, int Y, int* Array)
    {
       ...
    }
    
    ...
    
    int X;
    int Y;
    int Array[123];
    
    Function(X, Y, Array);
    Or, since this would accurately fit your function definition, we could do something like:
    Code:
    void Function(int X, int Y, int* Array)
    {
       ...
    }
    
    ...
    
    int X;
    
    Function( X, X, &X );
    After all, it just wants a pointer to an integer...

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by quzah
    Or, since this would accurately fit your function definition, we could do something like:
    Code:
    void Function(int X, int Y, int* Array)
    {
       ...
    }
    
    ...
    
    int X;
    
    Function( X, X, &X );
    After all, it just wants a pointer to an integer...

    Quzah.
    Eh? He wanted a function that takes a coordinate (X & Y) and an Array, not the same parameter 3 times???
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Magos
    Eh? He wanted a function that takes a coordinate (X & Y) and an Array, not the same parameter 3 times???
    Your function takes two integers and a pointer to an integer. As such, the way I called your function is valid.

    You really want:

    void Function( int, int, int[] );

    I was simply pointing out that a pointer is not the same thing as an array.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    Ok with the integers.

    But now with the array,

    First of all there is no relationship between the array and the integers and i will not work with pointers.

    I have in my lib.h file an array called

    myarray[2][3] = {1,2,3,
    4,5,6
    7,8,9}

    now i want to load

    void function(int x, int y, my_new_array) {

    printf ... my_new_array[1][1];
    }


    function(1,3,myarray);


    Is that correct? Is the myarray being loaded into the funtion with the name i want inside the function??? That is because i want to use the function several times such as

    function(1,3,array1);
    function(1,3,array2);

    where array1 is different from the array2, but inside the function to be able to handle the array with a specific name.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Inside a function, any variable you pass it is accessed inside the function with whatever name you give it in the definition.

    Thus:
    Code:
    void function( int x )
    {
        printf("Using a variable I call x, it is %d.\n", x );
    }
    
    int main (void)
    {
        int a = 5, b = 6;
    
        function( a );
        function( b );
    
        return 0;
    }
    The same holds true for arrays, pointers, whatever. Keep in mind that functions do not execute "at the same time" (unless using threads which is far beyond what you're doing now, so don't worry about it).

    Things are done sequentically. As such, the first function call ends before the second ever starts.

    The function doesn't care about the name of anything you pass it.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    ok

    Thanks a lot!

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by quzah
    I was simply pointing out that a pointer is not the same thing as an array.
    Oh yes, at a technical level an array and a pointer is exactly the same thing. When passing an array like you do it is a pointer being passed, not the actual array. You can even repoint that array argument as if it was a pointer. Even set it to NULL. Try it!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Magos
    Oh yes, at a technical level an array and a pointer is exactly the same thing. When passing an array like you do it is a pointer being passed, not the actual array.
    Correct. However, IIRC, there is a difference as far as C is concerned with the two definitions. For example, you could specify a size of the array, where with a pointer, you cannot:

    void function( int * x );
    void function( int x[] );

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    My point or ference was I believeeither here. Actually, that's not the post I was refering to. I believe that the though was something similar to "if you declare a size, then it [the array] is a constant ...", and so it was my understanding that when you specified parameters in the function declaration, that there was a difference.

    To illustrate my though, I was under the impression that if you set parameters for an array, you cannot pass it arrays whose size don't match. Where as, with your pointer, you could pass it anything.

    As such:

    void fun( int * ); //any array would work
    void fun( int[5][5] ); //a 20x20 array wouldn't

    That was my impression. And that's actually what I was thinking about, multidimension arrays.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    #include <iostream>
    
    using namespace std;
    
    void Func(int Array[32])
    {
       Array[4] = 12345;
    }
    
    int main()
    {
    	int Array[128];
    	Func(Array);
    	cout << Array[4] << endl;
    	return 0;
    }
    Compiles for me... Not a nice coding though.
    Two dimensional arrays as you mentioned wouldn't work though.

    (Personally, I find two dimensional arrays worthless since you can write faster access algorithms using one dimensional arrays simulating them to be two dimensional, but thats's another story).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by vVv
    Your C++ source is of course by default offtopic. Nevertheless, I don't see the point you're trying to make.

    [---snip---]

    This outputs the same as sizeof( int * ) will, regardless of whether you compile it as C or C++...
    I believe they were illustrating my point. That for single dimension arrays, even if you declare the size of the arrray, you can pass it whatever you feel like and it doesn't care.

    My original thought was that I was thinking with a single pointer to a type, you could overrun the boundries. For some reason I was thinking that when using an array rather than a pointer, it was the better of the two.

    I should have taken a moment to realize that C doesn't care if you blow past the end of your array, it isn't going to stop you.

    Anyway, the point of confusion was that I was thinking of multidimension arrays, that you can't pass mismatched sized arrays, as per my last post.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Done!

    Ok, i redesigned it from the scratch and everything is ok.

    I needed the 2-dimensional arrays for this reason.

    I am displaying pictures like this in the image in a graphic lcd. So i take the .bmp and make it hex in a format i want(from Matlab) and so i want to store this code in an array. Ofcourse it could be done with 1D array but i think it is more pretty and clear with 2D.


    Something else. Is there a way i could get the dimensions of the array?

    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. 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
  3. Replies: 6
    Last Post: 07-21-2008, 06:04 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM