Like Tree1Likes
  • 1 Post By grumpy

problem with pointers and bidimensional arrays

This is a discussion on problem with pointers and bidimensional arrays within the C Programming forums, part of the General Programming Boards category; Hi all. I have a problem with a pointer and a bidimensional array. here's the code: Code: includes.. typedef char ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Location
    Mexico City
    Posts
    33

    problem with pointers and bidimensional arrays

    Hi all.

    I have a problem with a pointer and a bidimensional array.
    here's the code:

    Code:
    includes..
    
    typedef char t_board[5][4];
    int i,j;
    
    t_board *copy (t_board b) {
    t_board *ptr;
    
            ptr = (t_board *) malloc (sizeof(t_board));
            for (i=0; i<5; i++)
                for (j=0; j<4; j++)
                      *ptr[i][j] = b[i][j];
    
            return ptr;
    }
    
    int main () {
    t_board some_board, *new_board;
    
           new_board = copy(some_board);
                .
                .
                code here
                .
                .
    }
    ........

    After this, I print new_board, and its ok; but the following code after
    the call to copy, crashes. Don't know why.
    if I remove the statment new_board = copy(some_board); , the code that follows runs ok.
    Can someone tell me why?
    Thanks;
    If you want to be happy one hour: take a nap
    if you want to be happy one day: go fishing
    If you want to be happy a year: inherit a fortune
    if you want to be happy for a life time: HELP SOMEBODY
    chinisse say.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    > *ptr[i][j] = b[i][j];
    You have to watch the precedence here.
    You have a pointer to an array, not an array of pointers.

    So you need
    (*ptr)[i][j] = b[i][j];
    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.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Location
    Mexico City
    Posts
    33

    it still not working

    Quote Originally Posted by Salem View Post
    > *ptr[i][j] = b[i][j];
    You have to watch the precedence here.
    You have a pointer to an array, not an array of pointers.

    So you need
    (*ptr)[i][j] = b[i][j];

    the only difference between the code I posted, and the one i'm writting, is that some_board is now a parameter of a function.
    I think it shouldn't be a mistake.

    Code:
    void func(t_board b) {
    t_board *new board;
    
             new_board = copy(b);
    }
    If you want to be happy one hour: take a nap
    if you want to be happy one day: go fishing
    If you want to be happy a year: inherit a fortune
    if you want to be happy for a life time: HELP SOMEBODY
    chinisse say.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    Interesting.... do go on.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    typedef char t_board[5][4];
     
    t_board *copy (t_board b) {
      int i, j;
      t_board *ptr;
     
            ptr = malloc (sizeof(*ptr));
            for (i=0; i<5; i++)
                for (j=0; j<4; j++)
                      (*ptr)[i][j] = b[i][j];
     
            return ptr;
    }
     
    int main () {
      int i,j;
      t_board some_board = { 
        { 1, 2, 3, 4 },
        { 11, 12, 13, 14 },
        { 21, 22, 23, 24 },
        { 31, 32, 33, 34 },
        { 41, 42, 43, 44 },
      };
      t_board *new_board;
     
      new_board = copy(some_board);
      for ( i = 0 ; i < 5 ; i++ ) {
        for ( j = 0 ; j < 4 ; j++ ) {
          printf("%2d ", some_board[i][j] );
        }
        printf("\n");
      }
      for ( i = 0 ; i < 5 ; i++ ) {
        for ( j = 0 ; j < 4 ; j++ ) {
          printf("%2d ", (*new_board)[i][j] );
        }
        printf("\n");
      }
      free(new_board);
      return 0;
    }
    
    $ gcc -Wall bar.c
    $ ./a.out 
     1  2  3  4 
    11 12 13 14 
    21 22 23 24 
    31 32 33 34 
    41 42 43 44 
     1  2  3  4 
    11 12 13 14 
    21 22 23 24 
    31 32 33 34 
    41 42 43 44
    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.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5,869
    Quote Originally Posted by Gustaff View Post
    I think it shouldn't be a mistake.
    Unfortunately, the C standard (by virtue of the precedence of operators) disagrees with you.

    In a dispute between your expectations and behaviour of your compiler, the compiler almost always prevails. When the dispute is because your expectations differ from the standard, then there is almost 100% chance of your compiler prevailing.

    Salem has pointed you in the right direction.
    laserlight likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with handling pointers as arrays.
    By nick412 in forum C Programming
    Replies: 2
    Last Post: 02-14-2012, 09:40 AM
  2. 4 questions about bidimensional table
    By Dan. in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2011, 10:18 AM
  3. Pointers to arrays problem
    By key4life in forum C Programming
    Replies: 11
    Last Post: 02-13-2010, 04:56 PM
  4. Problem with arrays, pointers and functions when combined
    By The Wazaa in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2006, 09:44 AM
  5. Problem with arrays and pointers
    By Dupek in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2002, 11:23 AM

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