C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-21-2002, 03:41 AM   #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.
rfbu is offline   Reply With Quote
Old 06-21-2002, 05:15 AM   #2
Registered User
 
trekker's Avatar
 
Join Date: Mar 2002
Posts: 46
Re: pointer to pointer and 2D arrays

Quote:
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...
trekker is offline   Reply With Quote
Old 06-21-2002, 07:48 AM   #3
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,662
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; 
}
Salem is offline   Reply With Quote
Old 06-21-2002, 11:46 AM   #4
End Of Line
 
Hammer's Avatar
 
Join Date: Apr 2002
Posts: 6,240
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]
Hammer is offline   Reply With Quote
Old 06-23-2002, 07:24 PM   #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
rfbu is offline   Reply With Quote
Old 06-23-2002, 07:34 PM   #6
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
>"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.
Prelude is offline   Reply With Quote
Old 06-23-2002, 08:36 PM   #7
Registered User
 
Join Date: Nov 2001
Posts: 23
thanks

will try it and let you know how it goes
rfbu is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:22 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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