473,569 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2215
Li****@gmail.co m 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.co m 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 objektorientier ter Datenverarbeitu ng
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
17391
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 * is 4 bytes, should the following yield 4, 5, of something else? (And, if something else, what determines the result?) char x = "abcd"; printf(...
7
4349
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 type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
4
5378
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 literal. But the second printf seems to give me garbage. Any advise on what I am doing wrong? Thanx
22
17152
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
8788
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 a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below...
14
15012
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 a comma-delimited string or string array. I don't want it to be a string because I want to overload this function, and it's sister already uses a...
8
13939
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 the array can vary depending on the parameters passed. What I need to do is loop through the returned array and if applicable write the array...
17
4646
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 analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if(...
11
17634
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 s(i)=new string(test) Above line gives - error implicit conversion string to 1-dim array of
14
4051
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
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.