PASSINAG A bidimansional array at a function

This is a discussion on PASSINAG A bidimansional array at a function within the C Programming forums, part of the General Programming Boards category; how do I pass as referance a bidimansional array ata function? this int a[1][2]; void f(int **b); doesnt work ! ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    PASSINAG A bidimansional array at a function

    how do I pass as referance a bidimansional array ata function?

    this
    int a[1][2];
    void f(int **b);
    doesnt work ! type mismatch.

    -i found this way ...
    struct c
    {
    int *p[2];
    } array;

    voit initNewType()
    {
    array.p[0] = &(a[0][0]);
    array.p[1] = &(a[0][1]);
    }

    void f(c *value)
    { //how do i move the array index thru the bidimansioal array?????
    *((*value).p[i]+j); //value at a[j][i]....but there 's a mistake someone can help me?
    //there's a simpler solution instead that using this mess?

    }
    main()
    { f(&array);
    }

    ///there's a esyer way to pass by referance a duble array?


    if you emailme to bruno.marovelli@fastwebnet.it i'll really apprciate it!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    int a[1][2];
    void f(int b[1][2]);
    Use that until you're more comfortable with arrays.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    i'M CONFORTABLE WITH ARRAYS BUT...passing a duble array by referance at a function isn't a easy task that's all ,infact...all people respond me with performing a local copy...but this makes computing really slower and here is a fastnest matter topic.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays are never passed by value or copy.

    On an aside, why do you have a 1 for the size of one of your array dimensions? There's no point. Just use a single dimension array, because having 1 does nothing useful.
    [edit]
    Code:
    char array[1][2]; /*this, is in effect... */
    char array[2]; /* the same as this... */
    [/edit]

    Quzah.
    Last edited by quzah; 06-14-2004 at 05:50 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    No
    becouse the method must work for general dimension bidimensional arrays for example:
    int a[100][203]
    this array rappresent an image data for example and referance rewuired to make things faster.
    And then...coping the array the way,i didn't think today about it, but it's interesting ,this way:
    int a[12][2];
    f(int b[12][2]);
    main()
    { f(a);

    }

    however my first solution is faster...the one in the initial thread lquestion i posted...i need something that permets me to pass a bidimensional array by referance.
    Someone have the TC++ Borland Paper Guide? I haven't got it anymore...but there...must be an explanation how to make it!

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Arrays are always passed by reference - no matter how you dress them up

    Code:
    int a[12][2];
    
    // All these prototypes are equivalent
    f(int b[12][2]);
    f(int b[][2]);
    f(int (*b)[2]);
    
    int main()
    {
      f(a);
      return 0;
    }
    I wonder if you're trying to pass
    int a[10][10];
    int b[20][40];
    to the same function.
    If so, read this http://www.eskimo.com/~scs/C-faq/q6.19.html



    And use code tags!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    are you kidding?

    well not at all...do you think I'm stupid?
    Can you understand what I wrote?
    Sure It's not possible to pass different bidimesiona arrays with to a function defined f(ina a[2][3]) becouse c++ perform a array member to member copy offcourse!
    But your suppositions letme think you don't understend pretty much what you talk about or reading.....:-|

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    no need something more fine working

    However your solution again doesent perform a by referance passing to the function but performs a copy of the passed array to the local array to the function and if the array passed ad declared in the function proptipe is quite huge time CPU computing an memory space is widely wasted!
    That's not high performed coding...and sound really stupid!

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    and array aren't passed by referance.At least that you specifically specify that.
    Infact you shouldent define a prototipe so:
    f(int b[4][4]);
    int a[4][4];
    to pass an array.. infact
    int a[4];
    f(int *b);
    main()
    {
    f(a); //this is a by referance passing of the a array
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No it doesn't. You're wrong.
    Code:
    void foo( char bar[] )
    {
        bar[0] = 'N';
        bar[1] = 'o';
        bar[2] = ';';
    }
    
    int main( void )
    {
        char array[] = "Hi! You're still wrong."
    
        printf("%s\n", array );
        foo( array );
        printf("%s\n", array );
    
        return 0;
    }
    Run that.

    So you're telling me that this is going to:
    1) Allocate the first array.
    2) Call the function.
    3) Make a duplicate of the first array inside the function.
    4) Copy everything from the first array into the new local array.
    5) Modify the local array.
    6) Copy everything from the new local array back into the first array.

    Is that what you're trying to say? Well, you're wrong. I know what it's like to want to be right. But trust us on this one, you're wrong. I suppose someone could quote the standard, but I doubt you'd believe that either.

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

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    no !
    In your wxample there's no local copy but a reference char array passing ...so the two printfs will output different results!
    But
    int a[2][3];
    void f(int b[2][3]);

    main()
    {
    f(a); //will not modify a if computational is performed inside f on b

    }

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    undefinedundefinedundefined
    there 's a local copy

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >f(a); //will not modify a if computational is performed inside f on b

    Have you tried it?

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    Well
    ok,it's right...but how do I solve this?
    a[may chage][may chage]

    f(b[must thake in count that change][must take in count that change]);
    ????

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    #include <stdio.h>
    char a[2][4] = {"Not", "So"};
    
    void f(char b[2][4]) {
      b[1][2] = '!';
      b[0][1] = '(';
    }
    
    int main(void)
    {
      puts(a[0]);
      puts(a[1]);
      f(a); //will not modify a if computational is performed inside f on b
      puts(a[0]);
      puts(a[1]);
      return 0;
    }
    Run that and learn why you are wrong.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 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, 09:18 PM
  3. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 01:19 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

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