Thread: pointer to pointer and 2D arrays

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    23

    pointer to pointer and 2D arrays

    is there a way to initialise a pointer to pointer to the 2D array?

    char **p;
    char (*q)[5];

    I mean a pointer to an array to a 2D array, I know that. but what about the above? I was trying it out but I kept getting errors.

    In case anybody is wondering, I'm just curious whether it's possible so that I could use or understand it when the time comes.

    Please feedback.

  2. #2
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46

    Re: pointer to pointer and 2D arrays

    Originally posted by rfbu
    is there a way to initialise a pointer to pointer to the 2D array?

    char **p;
    char (*q)[5];

    Please feedback.
    the above definitions are almost comletely different

    char **p;
    p is a pointer to pointer to char
    you may use that as a 2D array with the proper
    memory allocations.

    char (*q)[5];
    q is pointer to an 1-D 5 element char array.

    hope this helps...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Any use?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( ) {
        int     arr[10][20];    // a 2D array
        int     (*pa)[20];      // a pointer to a 2D array
        int     **b;            // a pointer to a pointer
        int     cols;
    
        // simple point at the given array
        pa = arr;
    
        // you can also call malloc to initialise
        // this synthesises arr[30][20]
        // the major size can be anything you want, but
        // the minor size is stuck at 20 at compile time
        pa = malloc ( 30 * sizeof(pa[0]) );
    
        // for total flexibility
        // this also allocates arr[30][20]
        // but you can choose all dimensions at run-time
        b = malloc( 30 * sizeof(b[0]) );
        for ( cols = 0 ; cols < 30 ; cols++ ) {
            b[cols] = malloc( 20 * sizeof(b[0][0]) );
        }
    
        return 0; 
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There's also this to read, compliments of Salem.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    23
    thanks for the hints and tips.

    however, I compiled the code but I got an error

    "ANSI C++ forbids implicit conversion from 'void *' in assignment"

    for this line

    /code/ pa = malloc( 30 * sizeof(pa[0]) ); /*code/

    by the way, my compiler is Dev-C++ 4.9.3.0

    so I tried this...this may sound silly, but get ready ...

    /code/ pa = (int **)malloc( 30 * sizeof(pa[0]) ); /*code/

    and I got this error,

    /errmesg/ assignment to 'int (*)[20]' from 'int **'/errmesg/

    I'm still trying to get a 'firm' grasp of pointers, any help is appreciated.

    thanks in advance

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >"ANSI C++ forbids implicit conversion from 'void *' in assignment"
    Then compile this as C code instead of C++.

    >assignment to 'int (*)[20]' from 'int **'
    pa isn't declared as int **, it's declared as int (*)[20] and all usage should reflect that, including casts.

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

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    23
    thanks

    will try it and let you know how it goes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2d Int Arrays
    By Voondebah in forum C Programming
    Replies: 2
    Last Post: 02-25-2009, 11:36 PM
  2. 2D Dynamically allocated pointer arrays
    By Lionmane in forum C Programming
    Replies: 37
    Last Post: 06-11-2005, 10:39 PM
  3. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 10:58 AM
  4. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  5. Passing 2D arrays by reference
    By samGwilliam in forum C++ Programming
    Replies: 9
    Last Post: 04-27-2002, 12:20 PM