-7
char charArray[10];
char* pArray = charArray;
for(int i = 0; i < 10; i++)
{
*pArray = '\0';
pArray++;
}

From this fragment, what does the *pArray = '\0'; mean? I cannot understand this.

Webtm
  • 87
  • 5

3 Answers3

10

Before the loop pointer pArray points to the first character of array charArray due to its initialization.

char* pArray = charArray;

So inside the loop at the first iteration *pArray yields reference to the first character of the array. Thus the referenced character is assigned by character literal '\0'.

*pArray = '\0';

Then the pointer is incermented

pArray++;

and points to the next character. So at the next iteration the second character of the array will be set to '\0'. As the loop has 10 iterations then all 10 characters of the array will be initialized by '\0'.

This could be done much simpler in the array definition

char charArray[10] = {};

In this case also all 10 characters of the array will be initialized by '\0'.

In C the equivalent construction will look as

char charArray[10] = { '\0' };

Take into account that if the variable pArray is needed only inside the loop to initialize the elements of the array then it is better to write the loop the following way (if you want to use a pointer):

const size_t N = 10;
char charArray[N];

for ( char *pArray = charArray; pArray != charArray + N; ++pArray )
{
  *pArray = '\0';
}

In fact this loop is equivalent to the internal realization of standard algorithm std::fill that you could use.

For example

const size_t N = 10;
char charArray[N];

std::fill( charArray, charArray + N, '\0' );

Or you could use standard functions std::begin and std::end to specify the first and the last iterators fot the array.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    This answers the question literally, but answering questions literally makes little to no sense with such horrible questions. – Griwes Jul 20 '14 at 20:33
  • 4
    @Griwes It is not a horrible question. And the answer has a great sense because it ANSWERS THE QUESTION and gives understanding of C++ constructions and definitions. I think that the problem is not with the question but it is with you. – Vlad from Moscow Jul 20 '14 at 20:36
  • 3
    @Griwes: The code does make sense. To say it doesn't is incorrect. Are you suggesting incorrect answers are better than correct ones? – Benjamin Lindley Jul 20 '14 at 20:39
  • 4
    @Griwes There is not enough information in the question to determine whether it is a good idea to use `std::string`. And simply suggesting that without explaining the implications wouldn't be too good either. – juanchopanza Jul 20 '14 at 20:39
  • @BenjaminLindley, I am saying "this code makes no sense, don't try to understand it" is better than "this code does this and this" when it comes to code as horrible as this abomination we can see in the question. – Griwes Jul 20 '14 at 20:39
  • @Griwes By the way I would like to mention that for example char charArray[10] = {}; is not a C construction:). – Vlad from Moscow Jul 20 '14 at 20:41
  • 10
    @Griwes It seems that my answer to the question is also very useful for you. Now you will know that the mentioned above construction is not a valid C construction C does not allow an empty initializer list.:) – Vlad from Moscow Jul 20 '14 at 20:46
  • @VladfromMoscow, ok, point taken here; apparently GCC only *warns* about this at -pedantic, which is ultimately retarded. – Griwes Jul 21 '14 at 00:28
5

Here's a better alternative to that terrible code, if you want to use strings:

std::string str;

Alternatively, if you really want an array of 10 characters, you can use:

std::array<char, 10> arr;
arr.fill('\0');

If you want a resizable array that will initially contain 10 characters, use:

std::vector<char> vec(10, '\0');
Shoe
  • 74,840
  • 36
  • 166
  • 272
3

The character literal '\0' is a NUL character with the value of 0x00. This loop zeros the array.

Khouri Giordano
  • 1,426
  • 15
  • 17