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, 19h48   #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, 19h50   #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, 20h00   #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, 20h09   #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, 20h30   #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, 20h31   #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, 21h57   #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, 01h21   #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
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 07h29.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, 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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12240 seconds with 16 queries