Will this code work? (simple)

Hello,
Today when creating a code for Nano I got this doubt,
will the following code work?

#define Cookie      3

#define BigCookie 4

//Sorry for the cookies, I'm just a little hungry :)


void setup() 
{
  pinMode(Cookie, OUTPUT);
  pinMode(BigCookie, OUTPUT);
}

void loop()
{
  char Z[] = {"Cookie", "BigCookie"};

 int i;

 for(i = 0; i < 2; i++)
 {
   digitalWrite(Z[i], HIGH);
 }

  delay(1000);
}

Would this code work in order to set “HIGH” value in the pins defined as “Cookie” and “BigCookie”?

That code won't even compile.
But the compiler already told you that.

Please use code tags when posting code - if you read back what you posted, you'll see that it doesn't match what you wrote.

No.

Most of the statements in your loop() are nonsense.

Z is one dimension array so it cannot hold 2 strings (you would need to do char Z[20] ...)

...

Okay so by fixing it up…

#define Cookie      3

#define BigCookie 4

char Z[][20] = {"Cookie", "BigCookie"};

void setup() 
{
 pinMode(Cookie, OUTPUT);
 pinMode(BigCookie, OUTPUT);
}

void loop()
{

 int i;

 for(i = 0; i < 2; i++)
 {
   digitalWrite(Z[i], HIGH);
 }

 delay(1000);
}

Thanks for the tip AWOL and arduino_new!

digitalWrite(Z[ var1 ], HIGH);Please think.
Hard.

And listen to what the compiler is telling you.

Also, what is the value of var1?
(No, I don't know either)

Please remember to use code tags when posting code.

Oh I’m sorry, I’m fixing that already

#define Cookie 3

#define BigCookie 4

char Z[][20] = {"Cookie", "BigCookie"};

void setup() 
{
 pinMode(Cookie, OUTPUT);
 pinMode(BigCookie, OUTPUT);
}

void loop()
{

 int i;

 for(i = 0; i < 2; i++)
 {
   digitalWrite(Z[i], HIGH);
 }

 delay(1000);
}
digitalWrite("Cookie", HIGH);

Does that make any sense?

Well not at all, it should not have the "", am I right this time?

So I should declare the string the following way:

char Z[][20] = {Cookie, BigCookie};

?

The pin names as strings do not have any meaning after compilation.

Try

#define Cookie      3

#define BigCookie 4

byte Z[] = {Cookie, BigCookie};

Perfect!

This Data Type / Keyword is available only in Arduino IDE or also in general C Language (when pointed to programme microcontrollers)?

Thanks a lot for your help AWOL :slight_smile:

This is just plain old C.

Nice.