|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Let's say I have a 2D array like this:
const double data[100][2] = { ... }; If I want to assign that array to another pointer variable, I can do this: typedef double PT[2]; const PT *dataptr = data; How would I declare "dataptr" there without using that typedef? I can't figure out the syntax, I've been trying all kinds of weird things with no luck. There's no reason why I have to not use a typedef, mostly I'm just curious (i.e. not looking for alternatives). Thanks, Jason |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Feb 25, 2:48 pm, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.com> wrote: > Let's say I have a 2D array like this: > > const double data[100][2] = { ... }; The first dimension (the "100") is arbitrary, I want dataptr to be able to point to any [][2] sized array. Sorry I forgot to say that. Jason |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
jason.cipriani@gmail.com wrote:
> Let's say I have a 2D array like this: > > const double data[100][2] = { ... }; > > If I want to assign that array to another pointer variable, I can do > this: > > typedef double PT[2]; > const PT *dataptr = data; > > How would I declare "dataptr" there without using that typedef? I > can't figure out the syntax, I've been trying all kinds of weird > things with no luck. There's no reason why I have to not use a > typedef, mostly I'm just curious (i.e. not looking for alternatives). > ... const double (*dataptr)[2] = data; -- Best regards, Andrey Tarasevich |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Feb 25, 3:00 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote: > const double (*dataptr)[2] = data; Thanks. Is that just a matter of memorizing things, or is there some rule that you can use to figure out how to declare types like that? Function pointers are kind of similar... they're really weird looking. I remember it took me a long time to memorize how to declare function pointer types, and in the end it was just a memorized syntax, I can't get my head around why the parentheses are there and why the variable name is in the middle of all that stuff... like, I can't intuitively parse that. The first type I tried was this: const double[2] * dataptr = data; The logic was: if "X *" can be a pointer to an array of X then "double[2] *" is a pointer to an array of double[2]'s. That makes sense in my head but isn't the right syntax. On the other hand, "const double (* dataptr)[2]" doesn't make sense to me when I look at it. I guess this is a pretty vague question, but is there some rule there, or some method to the madness? Thanks, Jason |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
jason.cipriani@gmail.com wrote:
> On Feb 25, 3:00 pm, Andrey Tarasevich <andreytarasev...@hotmail.com> > wrote: >> const double (*dataptr)[2] = data; > > Thanks. Is that just a matter of memorizing things, or is there some > rule that you can use to figure out how to declare types like that? I always thought there was something in the FAQ. If there isn't, there were many attempts to describe the reading of declarations that you should be able to find in archives. Just google for "how to read C++ declarations" > [..] V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
jason.cipriani@gmail.com wrote:
> I > guess this is a pretty vague question, but is there some rule there, > or some method to the madness? Well, it is often called "right-left-inside-out" rule or something like that http://www.ericgiguere.com/articles/...l?noprint=true -- Best regards, Andrey Tarasevich |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Feb 25, 3:31 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote: > Well, it is often called "right-left-inside-out" rule or something like that > > http://www.ericgiguere.com/articles/...ons.html?nopri... Great link; thanks! On Feb 25, 3:30 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote: > I always thought there was something in the FAQ. I had thought I remembered seeing something like it too once; but I didn't have any luck finding it again. Still, the other article has a lot of really good info in it. Thanks again, Jason |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On Feb 26, 9:09 am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.com> wrote: > On Feb 25, 3:00 pm, Andrey Tarasevich <andreytarasev...@hotmail.com> > wrote: > > > const double (*dataptr)[2] = data; > > Thanks. Is that just a matter of memorizing things, or is there some > rule that you can use to figure out how to declare types like that? if you have some declaration declaring x as type T, then you can make substitutions: - changing x to (*y) means y is a pointer to T - changing x to y[N] means y is an array(N) of T - changing x to y(A) means y is a function taking parameters A and returning T Note, N could be blank for an incomplete array type, and A is a (possibly empty) list of types. For example, if you aren't sure how to make x be a pointer to an array[N]; first write the syntax for y being an array[N], and then replace y with (*x) . Another example: char (*d[24])(void); you could build this up: char a; char b(void); char (*c)(void); char (*d[24])(void); and the meaning is : 1. a has type: char 2. b has type: function taking void returning typeof(a) 3. c has type: pointer to typeof(b) 4. d has type: array[24] of typeof(c) so d is an array[24] of pointers to function taking void returning char. |
|
![]() |
| Outils de la discussion | |
|
|