Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Specifying array types.

Reply
Thread Tools

Specifying array types.

 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      02-25-2008
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
 
Reply With Quote
 
 
 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      02-25-2008
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
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-25-2008
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
 
Reply With Quote
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      02-25-2008
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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-25-2008
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


 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-25-2008
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
 
Reply With Quote
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      02-25-2008
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
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      02-27-2008
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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-27-2008
On Feb 27, 2:21 am, Old Wolf <oldw...@inspire.net.nz> wrote:
> On Feb 26, 9:09 am, "jason.cipri...@gmail.com"


> 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 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


Beyond that, you need to know the precedence and the
associativity: basically, the operators to the right have
precedence over those to the left, and associativity is always
with the nearest operators being handled first: left
associativity for those to the right, and right associativity
for those to the left. Finally, you can add parentheses to
change the precedence, or just because you feel like it (or to
make the precedence clearer).

For declaring a pointer to an array, as here, you need the
parentheses because the array "operator" is to the right, and
thus has precedence:
int * a [10] ;
is an array of pointers, i.e.:
int * (a[10]) ;
and not:
int (*a) [10]

The real fun, of course, starts when you start getting functions
(and pointers to functions) which return a type requiring one of
the operators to the right. It's illegal for a function to
return an array, but it can return a pointer to one:

int (*f( params ))[ 10 ]
{
}

I'd definitely recommend a few typedef's in such cases.

> 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) .


I think this will always work, because as long as you have the
pointer operators in parentheses, you'll never need other
parentheses. But it will result in more parentheses than needed
in some of the more common cases, e.g. an array of pointers will
be: first make a y which is a pointer -- (*y) -- then replace
the y with a an x which is an array -- (*x[10]). You'll end up
with:
int (*x[10]) ;
and not many people would normally use the parentheses there.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      02-28-2008
On Feb 27, 10:05 pm, James Kanze <james.ka...@gmail.com> wrote:
> On Feb 27, 2:21 am, Old Wolf <oldw...@inspire.net.nz> wrote:
> > 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 means y is a pointer to T.


Substituting (*y) for x, always makes y a pointer to T.

Substituting *y could change the meaning of the declaration
in other ways, e.g.:
int x[10]; // array[10] of int
int (*y)[10]; // pointer to array[10] of int
int *y[10]; // not pointer to array[10] of int

Hence why I do not advocate making that substitution.

> Beyond that, you need to know the precedence and the
> associativity


I don't see why. Can you give an example of a
declaration that can't be solved using my method?

> int * a [10] ;


This is int *b; and the b substituted out for a[10],
so a is an array[10] of what b was, i.e. pointer to int.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Q: Re-ordering array by specifying first-entry Tuxedo Javascript 4 09-01-2008 06:03 PM
Specifying global array size with const int Bilgehan.Balban@gmail.com C Programming 4 09-02-2005 08:46 AM
Specifying vector length in the function output valentin tihomirov VHDL 1 06-28-2005 06:52 PM
Specifying generics in configuration valentin tihomirov VHDL 14 01-07-2004 05:27 PM
Specifying string array as an attribute in HTML tag Jeff 'Jones' Putz ASP .Net Building Controls 0 07-28-2003 12:21 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57