Home c++ as in C++ withdraw an array of rows

as in C++ withdraw an array of rows

Author

Date

Category

Segmentation Fault Error

I write

const char * mas [] = {"asdfsg",
           "1231252",
           "KJL, HJL,",
           "0898-0",
           "Uyiiyu",
           "----------"};
For (int a = 0; '\ 0'! = MAS [A]; A ++)
{
  COUT & LT; & LT; MAS [A] & LT; & LT; Endl;
};

at the output I get:

asdfsg 1231252 KJL, HJL, 0898-0 Uyiiyu
Segmentation Fault

If I understand correctly, the pointer goes beyond the array. How can this be fixed if I specify the number of rows, the program works properly, but how to do without counting rows?


Answer 1, Authority 100%

Exit from the cycle is made as if you go through the line, and you have an array of rows. You need to either explicitly set the length of the array (for (a = 0; a & lt; 6; a ++) ), or add the final element to the array, for example, an empty string, but in this case, the check on the end of the array will be look different:

const char * mas [] = {
  "asdfsg", "1231252", "kjl, hjl,", "0898-0", "uyiiyu", "----------",
  ""};
For (int a = 0; Mas [A] [0]! = '\ 0'; a ++) {
  COUT & LT; & LT; MAS [A] & LT; & LT; Endl;
};

Answer 2, Authority 25%

The cycle goes into infinity. You check the first symbol of the MAS [A] string on the equality of the zero terminator, which is fundamentally incorrect approach.
If you use C++ – you have the ability to use Stl Vector. Your code is written in C-style, which means: or define the size, or use the string + vector.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions