Thread: double pointer to 2 dimensional array

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    32

    Lightbulb double pointer to 2 dimensional array

    Code:
    int main()
    {
            int matrix[2][4] = { { 11,  22,  33, 99},
                                 { 44,  55,  66, 110},
                               };
            int **ptr = (int **) matrix;
            printf("  %d  ", **ptr) ;
            return 0;
    }
    
    The above pgm didn't give any result while I compile. Please explain it.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you mean by "didn't give any result while I compile". Do you mean you didn't get an executable? Or do you mean that you got an executable but you got no output? (In this case, your output is probably hiding in the front of your command prompt, since you didn't print a blank line after it.)

  3. #3
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    Hey!
    Firstly you need to allocate memory for the values. And this is bot the right way to work with with 2 dimensional arrays.

    Read Dynamic allocation of memory!
    something like

    Something like this
    Code:
    r dimension n rows,  J columns
      ///allocate memory for r[i][j], id[i], read2hap[i]
      r = (short unsigned int**)calloc(n, sizeof(unsigned short int*));
      if (r == NULL) exit(EXIT_FAILURE);
    
      for (i = 0; i < n; i++){
        r[i]  = (unsigned short int*)calloc(J, sizeof(unsigned short int));
    
      }
    cheers
    Last edited by satty; 12-07-2010 at 11:17 AM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    int main()
    {
            int matrix[2][4] = { { 11,  22,  33, 99},
                                 { 44,  55,  66, 110},
                               };
    
            int *ptr = &matrix[0][0];
            int **pptr = &ptr;
            printf("  %d  ", **pptr) ;
            return 0;
    }
    You Should not ignore warning; but, learn to fix the causes.
    Doing manual cast to remove the warnings IS NOT the first thing to try!

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Pointer & Two Dimension Array ....
    By gokulvs in forum C Programming
    Replies: 7
    Last Post: 09-09-2009, 11:04 PM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  4. double pointer to 2-dim array?
    By vex_helix in forum C Programming
    Replies: 2
    Last Post: 04-06-2004, 12:31 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM