Quick question

This is a discussion on Quick question within the C Programming forums, part of the General Programming Boards category; Quick question about 2D arrays and pointers. Trying to input information into a 4x4 array and when I use the ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Quick question

    Quick question about 2D arrays and pointers. Trying to input information into a 4x4 array and when I use the numbers 1-16 it repeats the numbers every time it goes to a new row. i.e.

    1 2 3 4
    4 5 6 7
    7 8 9 10
    10 11 12 13

    instead of storing the numbers 1-16.

    This is the code I'm trying, been to help websites and still having trouble.

    Code:
    void read_matrix(int *m)
    {
    int *p;
    for(p = m; p < m + 16; p++)
       scanf("%d", p);
    }
    What I was trying to do was treat it like a single dimension array as that is what my textbook said could work. Any suggestions would be great.

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Code:
    void read_matrix(int ar[][4]) {
    
    int i,j;
    
    for(i = 0; i< 4; i++)
    for(j = 0; j<4; j++)
    scanf("%d",&ar[i][j]);
    
    }
    hope this helps

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    Forgot to mention that we can only use pointer arithmetic, not subscripting. That's where I'm having the trouble

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    just cast the array to a pointer to int.

    Code:
    void read_matrix(int m[][4])
    {
        int *p = (int*)m, *end =p+16;
    
        while(p != end)
        {
              scanf("%d", p++);
        }
    }
    the other way is to do this

    Code:
    void read_matrix(int *m)
    {
        int *p = m, *end =p+16;
    
        while(p != end)
        {
              scanf("%d", p++);
        }
    }
    
    int main()
    {
        int array[4][4];
        read_matrix(*array);
    
        return 0;
    }
    either way, but the first way is probably better as it blinds the user to how the function works.

    the other way of treating it as a 2D array using pointer arithmetic is abit harder so i won't post unless u want to know.
    Last edited by Kyro; 10-31-2003 at 02:31 AM.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>What I was trying to do was treat it like a single dimension array as that is what my textbook said could work. <<
    Not really, you'll find. Flattening the array isn't good practice.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Heh, pointers to multi-dimension arrays really sorts the men from the boys IMO

    Scratch your noggin over this for a while...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void read_matrix(int ar[][4]) {
        int i,j;
        for(i = 0; i< 4; i++) {
            for(j = 0; j<4; j++) {
                scanf("%d",&ar[i][j]);
            }
        }
    }
    
    void read_matrix2(int (*arr)[4]) {
        int (*p)[4];    /* point to a whole row of arr */
        int *q;         /* point to the start of a row of arr */
        for ( p = arr ; p < arr + 4 ; p++ ) {
            for ( q = *p ; q < *p + 4 ; q++ ) {
                scanf("%d",q);
            }
        }
    }
    
    void print_array ( int arr[][4] ) {
        int i,j;
        for(i = 0; i< 4; i++) {
            for(j = 0; j<4; j++) {
                printf("%d ",arr[i][j]);
            }
            printf( "\n" );
        }
        printf( "\n" );
    }
    
    int main ( ) {
        int array[4][4];
        read_matrix(array);
        print_array(array);
        read_matrix2(array);
        print_array(array);
        return 0;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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