home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!destroyer!gatech!hubcap!mjs
- From: mjs@hubcap.clemson.edu (M. J. Saltzman)
- Subject: Re: pointer to multi D array. How?
- Message-ID: <1992Aug28.164452.14897@hubcap.clemson.edu>
- Organization: Clemson University, Clemson SC
- References: <RICHARD.92Aug27181915@CLYDE.ttt.kth.se> <28AUG199208202210@envmsa.eas.asu.edu> <1992Aug28.163945.14051@hubcap.clemson.edu>
- Date: Fri, 28 Aug 1992 16:44:52 GMT
- Lines: 47
-
- In article <28AUG199208202210@envmsa.eas.asu.edu> ptran@envmsa.eas.asu.edu (Phi-Long Tran) writes:
- >In article <RICHARD.92Aug27181915@CLYDE.ttt.kth.se>,
- >RICHARD@CLYDE.ttt.kth.se writes...
- >>In article <1992Aug26.050053.114348@zeus.calpoly.edu>
- >clin@zeus.calpoly.edu (Chihtsung Jeffrey Lin) writes:
- >
- >>> ... How do you declare a pointer to a multi-dimensional array? [H]ere
- >>> is what I did.
- >>> int A[5][5]:={1,2,3,......}, **x;
- >
- >> Make that:
- >> int A[5][5]={1,2,3,.....}, *x;
- >
- > Why not make an actual pointer to an array? It may make references
- >easier to code. For example, a pointer to the array A declared above would
- >be, "int (*ptrA)[5][5]". Then we could reference array A with ptrA as:
- >
- > ptrA = &A; /* Assign ptrA the address of array A. */
- > (*ptrA)[5][0] = 1; /* (*ptrA) is the array A. */
- >
- >The pointer to the array, as in the above, is useful for dynamically
- >allocating multi-dimensional arrays or changes the starting address of any
- >array.
-
- Actually, I think you want
-
- int A[5][5] = {...}, (*x)[5];
-
- That makes x a pointer to the same type as an element of A, namely,
- an array of 5 ints. Then
-
- x = A; /* equivalent to x = &A[0] */
-
- x[1][3] = 4; /* equivalent to *(*(x + 1) + 3) = 4 */
-
- are legal, and behave the way you would expect.
-
- In response to another thread, I thought this was one of the hardest
- aspects of C to learn--not so much pointers, but the relationship between
- pointers and arrays (also the relationship between pointers and array
- parameters in function calls).
-
-
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-