C ++ pointer pointer char initialization

I have doubts. In initializing this in C ++:

char** A_Function()
{
    char** charList = new char*[2];
    charList[0] = "abcde";
    charList[1] = "fghij";
    return charList;
}

There is no problem "when compiling this code," but I'm not sure about the behavior.

1 - char list: char * is on ok heap? 2 - charList [n_position] is on the heap or stack?

I do not understand what char * [2] realy means, does this mean: Its a static array that has a pointer to char on each of its elements?

If it is static, this array will be allocated on the stack, so is this array a huge error maker?

If I am right, how to allocate it in a heap?

+3
source share
3 answers

Perhaps the image will help:

alt text

A_Function, charList , . charList A_Function, - , , , , A_Function ( .. -).

, - , , .

+6

2 char * (). ( ), . , . , . , .

char *. , const char * .

+5

new char*[2], , :

new          dynamically allocate
new      [ ] an array
new      [2] of two
new char*[2] pointers to "char" objects

, char s. ( , new). charList. charList ( ) , .

charList[0] charList[1] , , "abcde" "fghij" . , .

+1

Source: https://habr.com/ru/post/1764091/


All Articles