multi-dimensional array and pointers??

C

chy1013m1

I am slightly confused as to how to reference multi-dimensional array
with pointers.
I've tried the following code, and I was able to reference 33 as
pptr[0][2]

int multi[2][3] = {{11,27,33}, {12,13,14}};
int (*pptr)[3] = multi;

I know that int (*pptr)[3] reads : pptr is a pointer to an array of 3
ints, so does it mean it is a
int* ? or is it a int** ?
 
C

chy1013m1

after doing some experiments:
int multi[2][3] = {{11,27,33}, {12,13,14}};
int (*pptr)[3] = multi;
int **pptest = &pptr;
int *xptr = pptr;
I figured pptr is of type int* , since I can reference it by *(xptr +
n), and changing initialization of pptest to int** pptest = pptr gives
segmentation fault.

is there a good tutorial on ptr-ptr ? or does it not have a lot of
application?
since most website only address pointer and array(single dimension)
 
E

Eric Sosman

after doing some experiments:
int multi[2][3] = {{11,27,33}, {12,13,14}};
int (*pptr)[3] = multi;
int **pptest = &pptr;
int *xptr = pptr;
I figured pptr is of type int* , since I can reference it by *(xptr +
n), and changing initialization of pptest to int** pptest = pptr gives
segmentation fault.

is there a good tutorial on ptr-ptr ? or does it not have a lot of
application?
since most website only address pointer and array(single dimension)

Reviewing Section 6 of the comp.lang.c FAQ

http://www.c-faq.com/

might prove helpful. Sections 4 and 7 may also
reward your attention.
 
A

Andrey Tarasevich

after doing some experiments:
int multi[2][3] = {{11,27,33}, {12,13,14}};
int (*pptr)[3] = multi;
int **pptest = &pptr;
int *xptr = pptr;
I figured pptr is of type int* ,

Wrong. 'pptr' is of type 'int (*)[3]', just as you declared it. The compiler
should normally issue at least a warning for an attempt to initialize 'xptr'
with 'pptr', although formally this in an error in C.
since I can reference it by *(xptr + n), and changing initialization

That doesn't prove anything. You just found another semi-legal way to "locate"
array elements in memory - by re-interpreting a 2D array as 1D array. This is
rather irrelevant to understanding the nature of 'pptr'.
of pptest to int** pptest = pptr gives
segmentation fault.

As should be expected.
 
T

Tosha

after doing some experiments:
int **pptest = &pptr;
int *xptr = pptr;

VS2005 C:
warning C4047: 'initializing' : 'int **' differs in levels of indirection
from 'int (**__w64 )[3]'
warning C4047: 'initializing' : 'int *' differs in levels of indirection
from 'int (*)[3]'

VS2005 C++:
error C2440: 'initializing' : cannot convert from 'int (**__w64 )[3]' to
'int **'
error C2440: 'initializing' : cannot convert from 'int (*)[3]' to 'int *'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top