double astrix 2D array signature question..

This is a discussion on double astrix 2D array signature question.. within the C Programming forums, part of the General Programming Boards category; i have a 2D integer array called arr. which is passed into a function as what1(arr,6,5) but the signature is ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    double astrix 2D array signature question..

    i have a 2D integer array called arr.
    which is passed into a function as
    what1(arr,6,5)

    but the signature is of the function is
    int what1(int **arr, int m, int n)

    so maybe there is a mistake and
    it should be what1(&arr,6,5) instead of what1(arr,6,5)
    ??

  2. #2
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    can i do arr[0][0] = 0; for arr
    if arr is passed instead of &arr
    will it save the change after the function ends??

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, both are wrong. A 2D array can not by simply passing it to a function, be turned into a pointer to pointer.

    If you post the whole code, it may be possible to tell you how to make it correct, but a pointer to pointer should contain an address of a pointer. Which is not the address of the first element in an 2D array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    its a question from a test:
    i was asks what what1(arr,6,5) expression will return
    ??
    arr is a 2D array which looks like this:
    0 1 1 0 1
    0 0 1 0 0
    0 0 1 1 1
    0 1 0 0 0
    1 0 0 1 1
    1 0 1 1 0

    Code:
    int what1(int **arr, int m, int n){
    int i, j, tmp, stam=0;
    
    for(i=0; i<m; i++)
    for(j=0; j<n; j++){
       tmp = what2(arr,i,j,m,n);
       if (tmp>stam) 
             stam = tmp;
    }
    return stam;
    }
    
    int what2 (int **arr, int row, int col, int m, int n){
    int i,j,tmp, stam=0;
    
    if (row < 0 || row >= m || col < 0 || col >= n) return 0;
    if (arr[row][col] == 0) return 0;
    
    arr[row][col] = 0;
    for(i=-1; i<2; i++)
    	for(j=-1; j<2; j++){
    		if(!i && !j) continue;
    tmp = 1 + what2(arr, row+i, col+j, m, n);
    		if (tmp > stam)  stam = tmp;
    	}
    arr[row][col] = 1;
    
    return stam;
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so the posted code doesn't actually say that it is a statically allocated (or statically sized) 2D array, simply that it IS a 2D array. It would work if the array is allocated dynamically. [Or the code produces the correct pointers to form an array of pointers to each row of the 2D array].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    be sure that the code doesnt have errors.


    whats dynamically allocated?

    is it using malloc to create the array??

    and staticaly allocated is saying int arr[5][6];

    correct?
    Last edited by transgalactic2; 04-09-2009 at 06:03 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Dynamically allocated -> using arr = malloc(...); arr[n] = malloc(...) rather than something like arr[5][6].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok so lets assume that its dinamically allocated

    why it passes the arr as
    what1(arr,6,5) instead of what1(&arr,6,5)

    can i do arr[0][0] = 0; for arr
    if arr is passed instead of &arr
    will it save the change after the function ends??

    what2
    has that line
    Code:
    arr[row][col] = 0;
    and i cant decide what happens at the beginning when arr[0][0] = 0;
    because arr is passed not &arr
    so the change which that line will make
    will not exist after what2 ends
    ??
    Last edited by transgalactic2; 04-09-2009 at 06:25 AM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because arr is (presumably) declared as int **arr? That would make sense, don't you think?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so because arr is passed and not &arr
    so i cant change arr itself
    only *arr stuff
    arr[0][0] is like saying *((*p+0)+0)

    so i can change every cell in the array

    so i can change the signature of the function to
    what1(int *arr, int m, int n)
    and it will work the same way
    correct?

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    does *(&arr) equals arr[0][0]
    ??

    i think its not true

    arr[0][0]=*(*arr))

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by transgalactic2 View Post
    so because arr is passed and not &arr
    so i cant change arr itself
    only *arr stuff
    arr[0][0] is like saying *((*p+0)+0)

    so i can change every cell in the array
    arr[0][0] is *(*(p+0)+0), but other than that typo your understanding of what you can and can't change is correct here.

    In addition to changing arr[0][0], you can also change arr[0]. If you wanted to add extra columns to each row, for example, you could call realloc() on all of the arr[i]'s and the change would remain after you return from the function.

    But you can't add rows. That would require you to change arr itself, and any change to it will be lost when the function returns as you mentioned.

    so i can change the signature of the function to
    what1(int *arr, int m, int n)
    and it will work the same way
    correct?
    If you only wanted to work on one row inside what1, I guess you could pass in a particular arr[row], but you'd lose the ability to do arr[row][col] inside that function. Since arr is a pointer to int, arr[row][col] wouldn't make sense since it requires two dereferences to get to a value. That doesn't work since is only a pointer, not a pointer to a pointer. You'd instead have something like

    what1(arr[row], num_cols, which_col);

    in main() and inside what1() you'd do

    arr[col] = 0;

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    if its a 2D array then the signature should be int**

    and we pass it as &arr

    but when we pass it as arr
    it makes no sense

    ??

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    Quote Originally Posted by transgalactic2
    and we pass it as &arr

    but when we pass it as arr
    it makes no sense
    What is the type of arr, and what is the type of &arr? Which type matches int**?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i remember in previous examples in this forum
    that inorder to use int** we need to pass &arr
    so we could make changes on *arr

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplying a 2D array by a 1D array
    By youngvito in forum C Programming
    Replies: 14
    Last Post: 06-12-2009, 04:50 PM
  2. Question Fortran like 2D array
    By TauWich in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2007, 07:26 AM
  3. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 03:56 AM
  4. 2d array question
    By JoshR in forum C++ Programming
    Replies: 5
    Last Post: 04-09-2005, 01:52 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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