N
N
Neusoon2020-06-19 22:19:51
C++ / C#
Neusoon, 2020-06-19 22:19:51

What is the best practice for simple strings in C?

Hello. I am new to C and I have one question that is difficult to find a definitive answer. At least due to the fact that I do not know all the subtleties. In C, a string is an array of characters. So what is the best practice. I have a program in which there is a lot of text, statistical and everything is clear with it. But here with dynamic lines I came to a standstill. There are about 20-30 of them, mostly they are not big. From 4 to 30-35 characters.
So here's the best way to do it. Declare each line with a pre-larger value. For example 35 characters. Or all the same dynamically allocate. The first method seems to me simpler and more reliable. But somewhere they write that this is bad practice, etc. In general, any advice on strings in C is generally interesting. Since the information in Rus. small in comparison with hype languages.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Saboteur, 2020-06-20
@Neusoon

In your case, it's better (and faster) to have a static string length

C
CityCat4, 2020-06-20
@CityCat4

Allocate dynamically, but assume a large enough value - kilobytes, for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question