What is the syntax for pointers
12.9 Pointers to pointers and string tables
"Pointer to pointer" is a difficult subject, but it is worth understanding. The syntax of pointers to pointers looks like this:
What does "pointer to pointer" mean exactly? You have a pointer that points to a pointer that points to a variable and can access that variable. In technical jargon, multiple indirection is used. In theory, it is also possible to use pointers to pointers to pointers and so on. In practice, however, such multiple indirections hardly make sense. Most of the time you use pointers to pointers, that is, two dimensions.
The main area of application of pointers to pointers is the dynamic generation of multidimensional arrays such as matrix calculations. But that's what I'll cover in Chapter 14, "Dynamic Memory Management."
Let's first look at an example on this complex topic:
In this example it is important that you use the double indirection operator (**) when changing the variable, just like when declaring the pointer to a pointer. You would have instead
Wrote the following:
the pointer ptr_ptr would point to memory address 100. And this is mostly somewhere in the nirvana of memory. As I said, this topic is taken up again in Chapter 14, and it will make a lot more sense to you there.
12.9.1 String tables
To make it even more complicated now, I want to add the string tables, which are not dissimilar to pointers to pointers (but are not the same!).
An example: The following string constants are to be sorted alphabetically (without using the header file
You remember that * ptr represents the same start address as ptr . And the same now also applies to:
So you have an array of pointers. And this would look like this in the example:
Here you have what is known as a string table. How can the individual strings of a string table be accessed now? Here is a small example:
Figure 12.18 Use of a string table
Access to the string table is similar to that of the multi-dimensional arrays. The first edition
puts the word "car" on the screen. It can therefore be assumed that
using the indexing operator to refer to the starting addresses of the individual strings. With the second statement
the name »erika« is displayed. This can be explained as follows: sort  represents the starting address of "America", ie "A". Then +2 is added after the field index. The pointer, which would have continued to point to the beginning of "America" without +2, now points to the third letter of the word, ie to "e". Or more precisely: to the address of "e". The format character% s then causes the string to be displayed on the screen from this address onwards. This will be the same for the next issue. The spelling
only serves to demonstrate that it can be done that way. Of course, this is easier to read with:
The program is to be demonstrated again using addresses:
Figure 12.19 Output of the addresses of a string table
The first three issues
the (start) addresses and contents to which the second pointer points were always used - which the output confirms. Then only the address of the first pointer is used:
The content is of course the same when using one pointer as when using two pointers. But when the program is compiled, both pointers have a different address. The output of
confirms all of this again. Each individual pointer therefore needs its own storage space and therefore its own address. I'll try to explain it differently again:
This points to the yth character in the xth string. The program looks like this:
or - as already known - like this:
This would refer to the 3rd character in the 2nd string, which corresponds to the "t" character from the "Auto" string.
Now this string table is to be sorted alphabetically. The whole line of text is not shifted and copied back and forth unnecessarily, only the pointers have to be put in the correct order:
This sorting algorithm is called »Selection Sort«. The following lines sort the fields with pointers:
First, the first element in the string table is compared with all the others. This will find the smallest element that is placed at the beginning. Then the second element is compared with all the elements in front of it. This continues until the last element in the string table. You can find more about the algorithms in Chapter 22.
The most important thing - as has already been mentioned several times - is that pointers are there for addresses and nothing else. For example means
nothing more than a char array with 500 char pointers. More precisely, each of these 500 pointers can e.g. B. point to a string (char array). Need proof? Here you go:
In this example, the start address of a string constant was transferred to the first three pointers. This was not possible with simple arrays. Of course, it is not yet possible to assign the start address of a text created during runtime in this way. To do this, you need knowledge of dynamic memory management.
When it came to sorting strings, the pointers to the starting address of the strings could be sorted much more effectively (faster) than if this were done with the entire string. This is due to the fact that only addresses on a string are used.
And instead of
to write, is this notation
much more effective and shorter. Here there are four pointers to a char array that point to the start address of each individual word.
The programmer has the following advantages when he uses string tables:
- The program becomes clearer with string tables.
- It saves storage space.
- The management of string tables is easier and more efficient.
- If a program is written in several languages, this can be localized more easily.
Here is an example of how you can use string tables effectively:
This is a simple listing that translates a program into the appropriate language with conditional compilation. In this example,
the language is set to English. This is also confirmed when the program is issued. If you now have to write a version for your Spanish colleague, all you have to do is search for the string table and translate and add the appropriate entries. This is how an international program is created without too much effort:
String tables can also be used to conveniently display error messages on the screen:
The individual error messages are accessed with the field index from No.  - . "Access denied - no rights", for example, is an error message .
After this section about pointers to pointers and the string tables, one or the other question will surely come up. Above all, the examples were always given with constant values. So in order to deal with the topic effectively and sensibly, you have to be patient a little longer until you get to (I repeat myself) dynamic memory management (Chapter 14).
How did you like the Openbook? We always look forward to your feedback. Please send us your feedback as an e-mail to [email protected]
- Israel has cars
- Which engine has a long crankshaft?
- Where is Kailash in the world
- What is a pixel rate
- How do countries choose their flags
- How was your stay in Ghana
- What is the core banking solution
- What is the correctional officer's average salary
- What is the technology stack behind Declara
- Diabetes can develop in animals
- Why is Microsoft still alive?
- How cold is Britain going
- Can defeat Naruto Hashirama in his prime
- Kakashi against 5 Kage who would win
- How is Randyll Tarly supposed to die?
- What is the most lyrical Eminem song
- Which film character best represents your partner
- What is the dark side of marketing
- Can you suggest a desktop wallpaper for me?
- Which fruit is known as bathroom fruit
- How can I get stronger without moving
- What is a BPM tool
- Why are impact wrenches better
- How do I check the grammar in Word