pointer to matrix

This is a discussion on pointer to matrix within the C Programming forums, part of the General Programming Boards category; Say I have a matrix (that is of dimension DIM) of shorts called m1 and I want to split it ...

  1. #1
    Unregistered
    Guest

    pointer to matrix

    Say I have a matrix (that is of dimension DIM) of shorts called m1 and I want to split it into 4 (2 by 2)'s:

    I need a pointer that will point to the short in

    m1[0][dim/2]

    I have tried the following:

    short **B=&(m1[0][dim/2]);

    // B is supposed to point to the short in m1[0][dim/2].
    // m1 was passed into the function through its address as a
    // short ***

    I tried to check if B is pointing in the correct spot by doing this:

    printf("%d ",B[0][0]);

    This however prints out an incorrect value. Any suggestions?

  2. #2
    Unregistered
    Guest
    This is how i would do it..

    Code:
    main(void){
    
       short numbers[10][MAX_ROWS]={0};
    
     /* pointer to a 2d array.  You must express how many dims there are */
       short (*pNumbers)[MAX_ROWS] = NULL;
    
      /* asign numbers to pointer to array */
       pNumbers= numbers;
    
       /* using the pointer */   
       pNumbers[9][9] = 9998;
    
       /* using none pointer */
        numbers[5][5] = 7777;
    
       /* both work fine, and you can view access values using both*/ 
       printf("\n%i", pNumbers[7][7]);
       printf("\n%i", numbers[9][9]);
    Cheers,
    G'n'R

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    short *p = &m1[0][dim/2];
    without trying it, i don't see why this shouldn't work. check it and see

    -Prelude
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    PRELUDE:

    What you have: short *p = &m1[0][dim/2];
    does work for me, but the thing is that I need p to be a short **.

    And since m1 is a short *** type, when I do
    short **p = &m1[0][dim/2];
    I get an incorrect value.

    Thanks.

  5. #5
    Unregistered
    Guest
    I'm a bit confused, if using a single dereference works then why do you need to use a pointer to a pointer?

  6. #6
    Unregistered
    Guest
    Because the statement is in a recursive function which has short** as an argument.

  7. #7
    Unregistered
    Guest
    Dont know if i am understanding this correctly but if you want a pointer to a pointer i think this is the correct way. However some of it is unclear to me too. Anyway else??

    Code:
    short **pp =NULL;
    short *p = &m1[0][dim/2];
    
    pp=&p;
    
    /* this is where i am unclear.  Why not the need to dereference the pointer??? But it works */
    
      pp[0][0]= 8;
    
     printf("%i\n", pp[0][0]);
    
       getch();
    Cheers
    G'n'R

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, 04:23 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 03:28 PM
  4. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 03:16 PM
  5. Help!! Tips on Matrix Calculator Program please!
    By skanxalot in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2002, 10:26 AM

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