string array v.s. int array

This is a discussion on string array v.s. int array within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I find strange result in the following program. 1. For string array, dereferencing it will result in the ...

  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
    22,268
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    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, 06: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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21