Array of char pointers

I am looking at some code that I did not write, and wanted to help understand its element. The code stores arrays of characters, creates pointers to these arrays (assigning addresses of arrays to pointers). It looks like it is creating an array to hold the addresses of the pointers to the characters, and I just need to clarify what I'm looking at exactly. I am also confused about using double (**) when creating an array.

I included a stripped-down and simplified example of what I look below.

char eLangAr[20] = "English"; char fLangAr[20] = "French"; char gLangAr[20] = "German"; char* eLangPtr = eLangAr; char* fLangPtr = fLangAr; char* gLangPtr = gLangAr; char **langStrings [3]= { &eLangPtr, &fLangPtr, &gLangPtr }; 

When using an array, they pass it as an argument to the function.

 menu (*langStrings[0]); 

So, the idea is that the value of the English character array is passed to the function, but I am having problems with how to do this. Do they pass the menu function a copy of the value stored in the langStrings function, at location 0, which will be the eLandPtr address? If someone could explain the process in English so that I could get around it, that would be great. It can only be because it was a long day, but it simply was not right in my head.

+5
source share
4 answers

You are correct that langStrings contains pointers to pointers to an array of characters. Therefore, each langString[i] points to a pointer. This pointer points to an array. This array contains the name of the language.

As others have noted, this looks a bit awkward. I will clarify:

char eLangAr[20] = "English"; - an array of 20 characters, and the name " English " is copied to it. I do not expect the eLangAr variable to ever contain anything other than this language name, so there is no need to use an array; the constant will be sufficient.

char **langStrings [3]= ... It’s enough to have only one indirectness (one *), since there is no need to ever have a pointer to anything else (randomly shuffle languages?).

in conclusion, it is sufficient to have the following:

 const char *langStrings [3]= { "English", "French", "German" }; 

(Note const because strings are now read-only constants / literals.)


The fact that this code can be useful is when these language names must be written differently in different languages. So "English", "French", "German" became "Engels", "France", "Dwits." However, then there is still one level of indirection too much, and it will be enough:
 char *langStrings [3]= { aLangArr, fLangAr, gLangAr }; 
+4
source

OK, like that. The designation ** ptrToptr means a pointer to a pointer. The easiest way to think about this is as a 2D matrix β€” dereferencing a single pointer allows the entire matrix to only have one row in the matrix. Splitting the second pointer after that will give one value in the matrix.

This announcement:

 char eLangAr[20] = "English"; 

Declares an array of length 20, type char, and contains the characters 'E', 'n', 'g', 'l', 'i', 's', 'h' '\ 0' so it is (possibly) null terminated, but not complete (at the end there are a few empty characters). You can set the pointer to its beginning using:

 char* englishPtr = &eLangAr[0] 

And if you play englishPtr, it will give the value "E". This pointer:

 char* eLangPtr = eLangAr; 

points to the array itself (not necessarily the first value).

If you look

 *langStrings[0] 

You will see that this means the contents (dereferencing *) of the pointer in langStrings [0]. langStrings [0] is the eLangPtr address, so dereferencing it gives eLangPtr. And eLangPtr is a pointer to an eLangAr array (which contains "English").

I assume that the function wants to be able to write in eLangAr, so point it to another word without rewriting "English" itself. He could just rewrite the characters in memory, but I think he wants to keep these words safe.

+1
source

** represents a pointer to a pointer. It is used when you need to save a pointer to another pointer.

The value of eLangPtr will be a pointer to eLangAr, which will be set to "English"

0
source

Here:

 char eLangAr[20] = "English"; 

an array is created. It has a capacity of 20 char and contains 8 characters - the word "English" and the termination of the NULL character. Since this is an array, it can be used in a context where a pointer is expected - due to decay from the array to the pointer, which will build a pointer to the first element of the array. This is done here:

 char* eLangPtr = eLangAr; 

This is the same as:

 char* eLangPtr = &eLangAr[0]; // explicitly get the address of the first element 

Now that char* represents a pointer to a character (which means that it points to a single char ), char** represents a pointer to a char* pointer.

Difference:

 char text[] = "Text"; char* chPointer = &ch[0]; char** chPointerPointer = &chPointer; // get the address of the pointer std::cout << chPointer; // prints the address of 'text' array std::cout << *chPointer; // prints the first character in 'text' array ('T') std::cout << chPointerPointer; // prints the address of 'chPointer' std::cout << *chPointerPointer; // prints the value of 'chPointer' which is the address of 'text' array std::cout << *(*chPointerPointer); // prints the first character in 'text' array ('T') 

As you can see, this is simply an additional level of indirection.

Pointers to pointers are used for the same reason as β€œfirst level” pointers - they allow you to take the address of a pointer and pass it to a function that can write something to it, changing the contents of the original pointer.

In this case, it is not needed, this would be enough:

 const char *langStrings [3]= { eLangPtr, fLangPtr, gLangPtr }; 

And then:

 menu (langStrings[0]); 
0
source

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


All Articles