Meaning of char array

Asked

Viewed 606 times

2

I am trying to port a program done in C to C++ and there is a statement of a array of char who didn’t understand.

I found the following:

char *matches[1+4];

If it were the following:

char *matches[5]; 

There is difference in these two statements?

  • If you’re going to behave properly, you have to change almost everything like @Maniero said. Char arrays as it is in this section hardly makes sense in c++ when you can use std::string. The reason for being 4+1 and not 5 is because you want to give the idea that there are 4 chars plus the terminator, and so it is clearer in terms of reading the code.

3 answers

3


No, they’re the same, in C or C++.

But they are statements of array in C, already in C++ it should be completely different. If it really will port to C++ it has to change everything. Otherwise you will be programming in C in the C compiler++.

I have no idea why the person made a sum and did not put the value 5 right away. You can speculate, as they did here, but they’re all nonsense (only the person who did it can explain, and maybe not even she, often people do things they don’t understand). He may have even confused it with terminator, but in this case it doesn’t even make sense, because the array is 5 pointers to texts that will have the terminator, not even the size of them.

You could do it:

array<string, 5> matches;

But most people will take it and do it:

vector<string> matches;

I put in the Github for future reference.

In most cases it is as or more appropriate.

You can do as in the original, it works, but know that it’s not porting to C++, it’s two very different languages, the issue is that the C++ compiler compiles almost all C codes, so people think it’s the same thing, if you want to port to C++ the above codes are the most likely candidates.

See more in Difference between Std::list, Std::vector and Std:array.

0

The two statements are identical, and declare an array of 5 pointers.

The form that uses "1+4" is probably a misconception (with no major consequences) because it gives the impression that it is making explicit the position of binary zero that terminates the strings in C, which makes no sense in this statement because what is being declared is not a string, rather an array of pointers.

As for the size itself I think that initially it should be moved as little as possible, only later when the ported application is already working correctly could be investigated the points where the change to idiomatic C++ could be made, that is, exchange "char []" by string, using STL containers and algorithms, etc.

0

In addition to the differences for C++ data types- where there are "real" arrays, unlike C, focusing on your specific doubt:

char *matches[1+4];

char *matches[5]; 

Are they equivalent in C? Yes - they are. The reason the author of the code has written in the first way is very likely to be more readable that he wants space for 4 characters and another 1 byte that is the string terminator (which is \x00).

For the purpose of code it is exactly the same, including most compilers will do the sum at compile time - the binaries for the two versions must be identical. It’s only really for the author to hit his eye and know "ah, this variable fits 4 characters as a string".

This is not a very widespread practice, and I don’t know if I would recommend it - anyway it’s just aesthetic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.