C ++ dynamically allocates pointer Arrays

Source: Internet
Author: User

C ++ primer 4.34 exercise requires that strings in the vector <string> be taken out, stored in another character array, and the first address of each character array should be stored in one character pointer array.

It is not difficult to dynamically allocate a character array.

 
Char * PTR = new char [N];
 
But if you want to assign a character pointer array (each element in an array contains a pointer to another character array), you need to change it a little.
 
I did this exercise first.CodePaste it and analyze the method of dynamically allocating the character pointer array:
/*************************************** * **************** C ++ primer 4.34 4.35 exercise ** Program Read a set of string-type data and store them in ** vector. Next, copy the vector object to a character pointer number ** group. Create a new character array for each element in the vector, and copy ** the data of the vector element to the corresponding character array, finally, insert the character pointer array to the pointer of the array. **************************************** * ***************/# Include "stdafx. H "# include <iostream> # include <string> # include <vector> using namespace STD; int _ tmain (INT argc, _ tchar * argv []) {vector <string> ivec; ivec. push_back ("Hello World"); ivec. push_back ("Hello everybody! "); Ivec. push_back (" Hello my family! "); Vector <string >:: size_type size = ivec. size (); vector <string >:: iterator itr = ivec. begin (); vector <string >:: iterator end = ivec. end (); vector <string >:: size_type vsize = ivec. size (); // the size of the vector char ** chars = new char * [vsize]; // dynamically allocate a character pointer array // There are two asterisks on the left of the value assignment operation, which can be understood as the element of chars itself is a char * type, // The new operator returns another pointer pointing to the character array, so it is Char ** chars // note that the right side of the value assignment cannot be new (char *) [vsize], no () syntax is added after new. Int I = 0; while (itr! = END) {string STR = * itr; // obtain the next stringconst char * strcontent = STR from the vector. c_str (); // convert string to a string of the C style, size_t strlength = strlen (strcontent); // evaluate the string length, does not contain the null character char * strarray = new char [strlength + 1]; // dynamically allocates an array chars [I] = strarray for the current string; // record the first address of the character array // chars [I] is equivalent to * (chars + I) for (int K = 0; k! = Strlength + 1; k ++) {// copy the string content to the new array. strarray [k] = strcontent [k] ;}++ itr; ++ I ;}for (Int J = 0; J! = 3; j ++) {// display the cout strings <chars [J] <Endl; Delete [] chars [J]; // release character array} Delete [] chars; // release character pointer array return 0 ;}

This Code is based on the number of elements in the vector <string>.Dynamically allocates pointer arrays. The syntax is:

 
Char ** chars = new char * [vsize]; // dynamically allocates an array of character pointers

At first, I wondered why there are two * operators on the left side of the value assignment statement. The name returns a pointer to the array. But then we thought, char ** chars can be understood as follows: char * (* PTR), that is, new returns a pointer to the array (* PTR ), each element is of the char * type. After that, I want to display the new character pointer array more clearly, so I wrote the following sentence.

 
Char ** chars = new (char *) [vsize];
At this time, the compiler reported an error. I checked some information. A better explanation is:New cannot be followed (),The C ++ standard does not have such a syntax. Therefore
 
Int * P = new (INT) [N];
 
All are errors.

When assigning values to the element of chars,

 
Chars [I] = strarray;

The first address of the character array is assigned to the I-th element of chars, which is equivalent

 
* (Chars + I) = strarray

When outputting a string, it is cout <chars [J] instead of cout <* chars [J], which is relatively basic, chars [J] removes the first address of a character array and outputs the content from this address to the First '\ 0, * chars [J] provides the element of the character array's first address, so only the first character of the array is output.

Finally, it is worth noting thatWhen deleting an array space, do not forget to delete [].

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.