Thread: string array v.s. int array

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    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;
    You are not dereferencing string arrays or int arrays, but a pointer to a null terminated string and a pointer to an int array. So, the usual thing that happens when you print a string and an int array happens.

    2. When dereferencing it twice (operator **), why the result is always the 1st element in both string array sample and int array sample?
    Now you are dereferencing a null terminated string and an int array. My understanding is that the arrays decay to pointers to their first element at this point, hence you get the first element of the respective arrays.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, well, think of your arrays as pointers. So whcar_t me[] is actualyl wchar_t* me.
    When you output me (or *me2_ptr which is char**, becomes char* like me), the functions will walk in memory, outputting every character it finds until it dinds '\0' (because you're giving it the address where the string is stored).
    If you do **me2_ptr, you get... char. So you pass a single char to the function.

    When it comes to non-strings, it's different. If you pass an address of a variable, that address is printed, not the values. So you need to pass in A int to print the actual value. Makes sense?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM