| |

VerySource

 Forgot password?
 Register
Search
View: 750|Reply: 3

Pointer array problem in C ++

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-7 17:20:01
| Show all posts |Read mode
int main ()

{
vector <string> svec;

    string str;

    // enter the vector element

    cout << "Enter strings: (Ctrl + Z to end)" << endl;

    while (cin >> str)

        svec.push_back (str);

    // create character pointer array

    char ** parr = new char * [svec.size ()];

    // handle vector elements

    size_t ix = 0;

    for (vector <string> :: iterator iter = svec.begin ();

                  iter! = svec.end (); ++ iter, ++ ix) {

        // create character array

        char * p = new char [(* iter) .size () + 1]; // String type supports .size () method

        // Copy the data of the vector element to the character array

        strcpy (p, (* iter) .c_str ());

        // insert a pointer to the character array into the character pointer array

        parr [ix] = p;
    }

    // Output the contents of the vector object

    cout << "Content of vector:" << endl;

    for (vector <string> :: iterator iter2 = svec.begin ();

                  iter2! = svec.end (); ++ iter2)

        cout << * iter2 << endl; // Iterative output needs to be dereferenced

    // print the contents of the character array

    cout << "Content of character arrays:" << endl;

    for (ix = 0; ix! = svec.size (); ++ ix)

        cout << parr [ix] << endl; //? ? ? Outputting a pointer means outputting what this pointer points to? ? ? ? ? ? ?

// release each character array

    for (ix = 0; ix! = svec.size (); ++ ix)

        delete [] parr [ix];

   // release the character pointer array

delete [] parr;

    return 0;

}

The code is as above. The question mark is the problem. If I enter the string: a, b, c, CtrlZ, and then output the Content of character arrays, the output is parr [ix]. But under VS2005, it outputs a, b, c, and if you change the code to * parr [ix], it still outputs a, b, c, which is very unclear. The first output should be a pointer, not a string Ah? Expert solution
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-7 22:54:01
| Show all posts
The pointer to char * is like this

char a [] = "hello world";
cout << a << endl;
    
int b [] = {1, 2, 3, 4, 5};
cout << b << endl;

result:
hello world
0xXXXXXX ---> array b first address
Reply

Use magic Report

1

Threads

19

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-8 07:45:01
| Show all posts
Learn,

Help you
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-8 11:09:01
| Show all posts
Bullet brother is right, are there any other exceptions ... I think C ++ is so easy to confuse
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list