string array v.s. int array

L

LinLMa

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
 
V

Victor Bazarov

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
 
J

James Kanze

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>[/QUOTE]
[QUOTE]
using namespace std;[/QUOTE]
[QUOTE]
int main()
{
wchar_t me[] = L"Hello World \n";[/QUOTE]

Here's probably part of your misunderstanding.  This isn't a
string array, but rather an array of characters (wchar_t).
[QUOTE]
wchar_t (*me2_ptr)[14] = &me;
wcout << *me2_ptr << endl;  // output Hello World
wcout << **me2_ptr << endl; // output H[/QUOTE]
[QUOTE]
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[/QUOTE]
[QUOTE]
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" 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top