2D Arrays and pointers

This is a discussion on 2D Arrays and pointers within the C Programming forums, part of the General Programming Boards category; Hi all need a hand with passing a pointer for a 2D array through to a function so the function ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212

    Question 2D Arrays and pointers

    Hi all need a hand with passing a pointer for a 2D array through to a function so the function can copy data into it. And can't figure out why it ain't workin'. all help and advice will be appreciated.

    Code:
    #include <stdio.h>
    
    void input(char **array) {
        FILE* user;
        char filename[FILENAME_MAX];
    
        int x, y;
    
        printf("Please specify a filename:");
        scanf("%s", filename);
        if ((user = fopen(filename, "r")) != NULL) {
            for(y=0; y++; y<20) {
                for(x=0; x++; x<20) {
                    **array[x][y] = fgetc(user);
                }
            }
        }
        else {
            printf("error opening file\n");
            }
    
    }
    
    int main() {
    
        char inputarray [20][20];
    
        input(&inputarray[][20]);
    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    I don't know if this matters or not, but I thought you were supposed to declare the function before main

    i.e. void input(char*);

    then put the actual funtion after main. You guys probaly know if this is neccesary or not. It is just the way I was told to do it.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Passing arrays is dead easy, you just copy the declaration of the array

    This is your array
    &nbsp;&nbsp;&nbsp;&nbsp;char inputarray [20][20];

    The prototype of the function is
    &nbsp;&nbsp;&nbsp;&nbsp;void input( char array[20][20] );

    As a side note, these two prototypes mean exactly the same thing, the only difference is the amount of typing
    &nbsp;&nbsp;&nbsp;&nbsp;void input( char array[][20] );
    &nbsp;&nbsp;&nbsp;&nbsp;void input( char (*array)[20] );

    The function definition follows the prototype
    &nbsp;&nbsp;&nbsp;&nbsp;void input( char array[20][20] ) {
    &nbsp;&nbsp;&nbsp;&nbsp;}
    Inside the function, you use array[x][y] in exactly the same way as you would if the code and the array were all declared in main.

    Finally, you call this function like so
    &nbsp;&nbsp;&nbsp;&nbsp;input( inputarray );
    No fluffy & * or [] required.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    want to use pointers and not copy the whole array.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > want to use pointers and not copy the whole array.
    All arrays are passed by reference - the particular style in which you declare prototypes does not affect this underlying reality.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to 2D arrays
    By bertazoid in forum C Programming
    Replies: 16
    Last Post: 02-26-2009, 04:46 PM
  2. 2d Arrays and pointers
    By Sal79 in forum C Programming
    Replies: 20
    Last Post: 06-02-2007, 09:53 AM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. pointers to 2d arrays
    By Diamonds in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2002, 06:58 AM

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