![]() |
| | #1 |
| Registered User Join Date: Nov 2001
Posts: 23
| pointer to pointer and 2D arrays 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 | |
| | #2 | |
| Registered User Join Date: Mar 2002
Posts: 46
| Re: pointer to pointer and 2D arrays Quote:
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 | |
| | #3 |
| and the hat of Jobseeking 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 | |
| | #4 |
| End Of Line 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 | |
| | #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 | |
| | #6 |
| Code Goddess 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 | |
| | #7 |
| Registered User Join Date: Nov 2001
Posts: 23
| thanks will try it and let you know how it goes |
| rfbu is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |