Z
Z
Zuoya2020-10-30 10:48:20
C++ / C#
Zuoya, 2020-10-30 10:48:20

What is the correct way to create an array of strings?

Good day!
What advantage does a two-dimensional array have over an array of pointers to these strings?
As for me, the array of pointers looks nice, but for some reason in the lessons that I saw use a two-dimensional array.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wataru, 2020-10-30
@Zuoya

Usually arrays are used to be traversed in a row. Modern processors work very well if the data is consecutive in memory. Therefore, a two-dimensional character array will run faster due to the cache. If these are pointers to strings, then each string lies anywhere.
However, if there are a lot of strings, they are large, you often refer to random ones, and they are given to you from the outside, then the pointer approach is preferable. In this case, when you create an array, you will not copy the strings. And the advantage of the cache in such work with the array is negligible.

G
GavriKos, 2020-10-30
@GavriKos

Most likely the reason is in the allocation of memory and where and when it will be allocated.
You can't call it an advantage - it's just a fact. In some cases, this will not be an advantage.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question