PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Specifying array types.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Specifying array types.

Réponse
 
LinkBack Outils de la discussion
Vieux 25/02/2008, 20h48   #1
jason.cipriani@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Specifying array types.

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
  Réponse avec citation
Vieux 25/02/2008, 20h50   #2
jason.cipriani@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Specifying array types.

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
  Réponse avec citation
Vieux 25/02/2008, 21h00   #3
Andrey Tarasevich
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Specifying array types.

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
  Réponse avec citation
Vieux 25/02/2008, 21h09   #4
jason.cipriani@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Specifying array types.

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
  Réponse avec citation
Vieux 25/02/2008, 21h30   #5
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Specifying array types.

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


  Réponse avec citation
Vieux 25/02/2008, 21h31   #6
Andrey Tarasevich
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Specifying array types.

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
  Réponse avec citation
Vieux 25/02/2008, 22h57   #7
jason.cipriani@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Specifying array types.

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
  Réponse avec citation
Vieux 27/02/2008, 02h21   #8
Old Wolf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Specifying array types.

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.
  Réponse avec citation
Vieux 27/02/2008, 10h05   #9
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Specifying array types.

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:james.kanze@gmail.com
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
  Réponse avec citation
Vieux 28/02/2008, 11h07   #10
Old Wolf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Specifying array types.

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.
  Réponse avec citation
Vieux 28/02/2008, 13h48   #11
tragomaskhalos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Specifying array types.

On Feb 25, 8:09pm, "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?


In addition to what others have said, I'd add that this is
one of those things that looks very complicated when spelled
out in an algorithm, but is a lot easier in practice - sort
of like riding a bicycle. You could informally summarise reading
a decaration as "start in the middle, start going right, and then
ricochet between parens". At least this is how I mentally model
it when I'm doing it !
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h35.


Édité par : vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16269 seconds with 19 queries