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 > string array v.s. int array
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
string array v.s. int array

Réponse
 
LinkBack Outils de la discussion
Vieux 11/12/2007, 05h52   #1
LinLMa@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut string array v.s. int array

Hello everyone,


I find strange result in the following program.

1. For string array, dereferencing it will result in the string
itself, but for int array, dereferencing it will result in the address
of the array;

2. When dereferencing it twice (operator **), why the result is always
the 1st element in both string array sample and int array sample?

Code:
#include <iostream>
#include <string>

using namespace std;

int main()

{
wchar_t me[] = L"Hello World \n";
wchar_t (*me2_ptr)[14] = &me;
wcout << *me2_ptr << endl;	// output Hello World
wcout << **me2_ptr << endl; // output H

int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 };
int (*pval)[10] = &values;
wcout << *pval << endl;  // output 0x0017f6f0
wcout << **pval << endl; // output 10

return 0;
}

thanks in advance,
George
  Réponse avec citation
Vieux 11/12/2007, 05h59   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string array v.s. int array

LinLMa@gmail.com wrote:
> I find strange result in the following program.
>
> 1. For string array, dereferencing it will result in the string
> itself, but for int array, dereferencing it will result in the address
> of the array;


Streams make special accommodations for a pointer to const char
(or wchar_t). When a char* (wchar_t*) is output, it is presumed
to point to a C string (a wide C string). Pointers to int do not
get the same special treatment.

To bring them both to the common denominator, cast both (*me2_ptr)
and (*pval) to (void*). You'll see the value of the pointer in
both cases.

>
> 2. When dereferencing it twice (operator **), why the result is always
> the 1st element in both string array sample and int array sample?
>
>
Code:
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> int main()
>
> {
> wchar_t me[] = L"Hello World \n";
> wchar_t (*me2_ptr)[14] = &me;
> wcout << *me2_ptr << endl; // output Hello World
> wcout << **me2_ptr << endl; // output H
>
> int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 };
> int (*pval)[10] = &values;
> wcout << *pval << endl;  // output 0x0017f6f0
> wcout << **pval << endl; // output 10
>
> return 0;
> }
>
>
>
> thanks in advance,
> George


HTH

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 11/12/2007, 10h57   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string array v.s. int array

On Dec 11, 6:52 am, Lin...@gmail.com wrote:
> I find strange result in the following program.


> 1. For string array, dereferencing it will result in the string
> itself, but for int array, dereferencing it will result in the address
> of the array;


> 2. When dereferencing it twice (operator **), why the result is always
> the 1st element in both string array sample and int array sample?


>
Code:
> #include <iostream>
> #include <string>
Code:

> using namespace std;

> int main()
> {
>         wchar_t me[] = L"Hello World \n";

Here's probably part of your misunderstanding.  This isn't a
string array, but rather an array of characters (wchar_t).

>         wchar_t (*me2_ptr)[14] = &me;
>         wcout << *me2_ptr << endl;  // output Hello World
>         wcout << **me2_ptr << endl; // output H

>         int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 };
>         int (*pval)[10] = &values;
>         wcout << *pval << endl;  // output 0x0017f6f0
>         wcout << **pval << endl; // output 10

>         return 0;
> }
> 


ostreams don't support output of an array. On the other hand,
array's do convert implicitly to pointers, and ostreams do
support output of pointers. In the case of pointers to
character types, there is a special overload, which allows them
to treat the pointer as the beginning of a C style string;
there's nothing similar for pointers to int, so you just output
the pointer.

As for dereferencing twice: that's just the way C (and thus C++)
works. Arrays are second class citizens in C: formally, there
is no indexing, but rather pointer arithmetic and
dereferencing---the expression "a[i]" is defined to be "*(a+i)".
And of course, *a is the same thing as *(a+0).

Because of such anomalies, it's better to avoid C style arrays
(and strings) as much as possible: just use std::wstring and
std::vector, and things will behave rationally.

--
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
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 17h37.


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