0

I am programming Arduino and I have trouble when using the following code

struct myStruct {
  char variable1[10];
  char variable2[10];
  char variable3[10];

  // Constructor
  myStruct() : variable1({'\0'}), variable2({'\0'}), variable3({'\0'}) {}
};

since I get the following error:

expected primary-expression before '{' token

What is the problem? How can I solve it?

Note: The \0 is used for handling null terminated strings.


BTW: Why the following code works and the above does not?

struct myStruct {
  char variable1[10];
  char variable2[10];
  char variable3[10];
} variable = {{'\0'}, {'\0'}, {'\0'}};;
4
  • could you use string? Commented Oct 1, 2013 at 10:54
  • @billz - No, I can't. Commented Oct 1, 2013 at 10:54
  • do you want to set the char array all zero or to an empty string only ? Commented Oct 1, 2013 at 10:55
  • @Mario - I updated the question. However, there are other members that I didn't report in the question. Commented Oct 1, 2013 at 11:06

3 Answers 3

2

Remove parens. Use braces only.

That is, instead of

variable1({'\0'})

write this,

variable1{'\0'}   //removed parens!

If you follow that, your code would look like this:

myStruct() : variable1{'\0'}, variable2{'\0'}, variable3{'\0'} {}

Any compiler that supports C++11 should be able to compile it.


In C++03, write this:

myStruct() : variable1(), variable2(), variable3() {}

That should work for this particular case. That is all you have : value-initialization. C++03 doesn't give you much freedom.

Sign up to request clarification or add additional context in comments.

5 Comments

@Backo: In that case, you cannot initialize your array like that.
So, how could I make? Thanks anyway.
The \0 is used for handling null terminated strings. Your code will work the same?
If I would like to make it explicit (that is, I would like to state the \0 in my code) how can I make?
@Backo: In C++03, you cannot do that in the initialization. Sorry. :(
0

In C++11, your code should work, although it has rather more brackets than strictly necessary.

In any C++, you could specify value-initialisation to zero-initialise the arrays:

myStruct() : variable1(), variable2(), variable3() {}

If you're being ultra-paranoid, and don't trust '\0' to be equivalent to zero, then you'd have to write to the arrays in the constructor body.

Why the following code works and the above does not?

Because it's always been possible to aggregate-initialise variables in a declaration. It's only since 2011 that it's been possible to do that in a member initialiser.

Comments

0

Assuming you just want to initialise the member variables to empty (C-style) strings, change:

  // Constructor
  myStruct() : variable1({'\0'}), variable2({'\0'}), variable3({'\0'}) {}

to:

  // Constructor
  myStruct() : variable1(""), variable2(""), variable3("") {}

Edit

Apparently some versions of gcc complain about this usage (see comments below) - this appears to be due to a bug in gcc.

9 Comments

The \0 is used for handling null terminated strings. Your code will work the same?
Yes, "" is equivalent to { '\0' }, i.e. it sets the first byte to 0.
@PaulR: Did you try that? I get "error: incompatible types in assignment of ‘const char [1]’ to ‘char [10]’"
If I would like to make it explicit (that is, I would like to state the \0) how can I make?
The code is valid. Seems to be a bug in gcc for a while.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.