473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7.  
  8. {
  9. wchar_t me[] = L"Hello World \n";
  10. wchar_t (*me2_ptr)[14] = &me;
  11. wcout << *me2_ptr << endl;    // output Hello World
  12. wcout << **me2_ptr << endl; // output H
  13.  
  14. int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 };
  15. int (*pval)[10] = &values;
  16. wcout << *pval << endl;  // output 0x0017f6f0
  17. wcout << **pval << endl; // output 10
  18.  
  19. return 0;
  20. }
  21.  

thanks in advance,
George
Dec 11 '07 #1
2 2199
Li****@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?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. wchar_t me[] = L"Hello World \n";
  7. wchar_t (*me2_ptr)[14] = &me;
  8. wcout << *me2_ptr << endl; // output Hello World
  9. wcout << **me2_ptr << endl; // output H
  10. int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 };
  11. int (*pval)[10] = &values;
  12. wcout << *pval << endl;  // output 0x0017f6f0
  13. wcout << **pval << endl; // output 10
  14. return 0;
  15. }
  16.  


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
Dec 11 '07 #2
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?
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
Expand|Select|Wrap|Line Numbers
  1.  
  2.         
  3.                 using namespace std;
  4.  
  5.  
  6.         
  7.                 int main()
  8. {
  9.         wchar_t me[] = L"Hello World \n";
  10.  
  11. Here's probably part of your misunderstanding.  This isn't a
  12. string array, but rather an array of characters (wchar_t).
  13.  
  14.         
  15.                         wchar_t (*me2_ptr)[14] = &me;
  16.         wcout << *me2_ptr << endl;  // output Hello World
  17.         wcout << **me2_ptr << endl; // output H
  18.  
  19.  
  20.         
  21.                         int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 };
  22.         int (*pval)[10] = &values;
  23.         wcout << *pval << endl;  // output 0x0017f6f0
  24.         wcout << **pval << endl; // output 10
  25.  
  26.  
  27.         
  28.                         return 0;
  29. }
  30.  
  31.  
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:ja*********@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
Dec 11 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
22
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
8
by: Jeff Johnson | last post by:
Hi, I've begun converting an ASP site over to .NET and I'm a novice at both the new platform as well as C#. I have a COM+ object that returns a string array when it is called. The size of...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.