Struct? what do I wrong?

I'm (for the first time) to use a struct variable...
but I get every time a Does not name a type error?

this is what I have:

struct Corner {
int X;
int Y;
int Z;
} ;
Corner Corners[8];
Corners[0].X = 0;

Hello
Try this to initialize the structured array:

struct Corner {
   int X;
   int Y;
   int Z;
}Corners[] {
{0,0,0}, 
};

Post a complete code that that we can try in the Arduino IDE. Use Code Tags.

Can you post the full sketch? It could be a problem with where you are declaring the struct.

That's an assignment statement. It must be within a function.

Compiles without error or warning for me:

struct Corner
{
  int X;
  int Y;
  int Z;
} ;

Corner Corners[8];

void setup()
{
  Corners[0].X = 0;
}

void loop() {}

Because you put the assignment statement inside a function. OP's (incorrectly posted) code snippet doesn't.

@johnwasser's code looked wrong to me, of course I never argue with success.

Turns out I'm just an old C programmer. That doesn't work in C. I found this looking around to learn up a bit, well to try anyway.

which g0es into some C and C++ differences as well as other improvements visited upon us over the years.

I will probably use

typedef struct 
{
  int X;
  int Y;
  int Z;
} Corner;

Corner Corners[8];

no matter what I manage to learn.

Octagon?

a7

It is very long.... :slight_smile:

O.. It needs to be in setup();
I had it before.... just like other variables?

maybe stupid question... but i had this...:

int NewX[12] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 30 , 0 , -30 , 0};
int NewY[12] = {0 , 0 , 0 , 0 , 20 , 0 , -20, 0 , 0 , 0 , 0 , 0};
int NewZ[12] = {30 , 0 , -30, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0};

struct hoek {
int X;
int Y;
int Z;
} ;
struct hoek Corners[8];
//Corners[0].X = 0;

I mean, the 3 Arrays... and I want it in one struct array... so I try it in the same place...

now, if I put the Corners{0].X=0 in Setup(0) I don't get any errors ....
?

When you declared your int arrays and initialized them, you did it in one go.

For your struct array, you didn't and the compiler doesn't like that.

and if I want it in that place...? is that possible? and how do I do that? (if it is possible)

struct hoek Corners[12] =
{
{0, 0, 30},
{0,0,0},
{0,0,-30},
{0,0,0},
{0, 20, 0},
{0,0,0},
{0,-20,0},
{0,0,0},
{30,0,0},
{0,0,0},
{-30,0,0},
{0,0,0}
};

thanks.... now it works... :slight_smile:

next question about structs....

is it also possible to give a struct result out of a function?
and yes, how?

so I mean, I have a function... start it with some values to the function... and I want back a X,Y,Z value?

Hello
That is referenced as call by reference.
Take a view into a C++ tutorial.
Have a nice day and enjoy coding in C++.

I don’t enjoy coding in C++ :wink: but 65 seconds of googling make me think

in C++ you can pass a struct to a function and a function can return a struct to the caller by using syntax indistinguishable from that used to pass and return ints.

If I’m wrong, I am sure someone will fix me up on that.

a7

Declare it as:
struct hoek Function() {}

For example:

struct hoek
{
  int X;
  int Y;
  int Z;
} ;

struct hoek Corners[12] =
{
  {0, 0, 30},
  {0, 0, 0},
  {0, 0, -30},
  {0, 0, 0},
  {0, 20, 0},
  {0, 0, 0},
  {0, -20, 0},
  {0, 0, 0},
  {30, 0, 0},
  {0, 0, 0},
  { -30, 0, 0},
  {0, 0, 0}
};

void setup()
{
  Serial.begin(115200);
  delay(200);

  struct hoek returnVal = Function(0);
  
  Serial.print(returnVal.X);
  Serial.print(", ");
  Serial.print(returnVal.Y);
  Serial.print(", ");
  Serial.println(returnVal.Z);
}

void loop() {}

struct hoek Function(int index)
{
  return Corners[index];
}

Or not, kinda. Odd that it was in this thread that I learned a tiny bit of C++...

Declare it as:

hoek Function() {}

Compiles and produces identical results:

struct hoek
{
  int X;
  int Y;
  int Z;
} ;

hoek Corners[12] =
{
  {0, 0, 30},
  {0, 0, 0},
  {0, 0, -30},
  {0, 0, 0},
  {0, 20, 0},
  {0, 0, 0},
  {0, -20, 0},
  {0, 0, 0},
  {30, 0, 0},
  {0, 0, 0},
  { -30, 0, 0},
  {0, 0, 0}
};

void setup()
{
  Serial.begin(115200);
  delay(200);

 hoek returnVal = Function(0);
  
  Serial.print(returnVal.X);
  Serial.print(", ");
  Serial.print(returnVal.Y);
  Serial.print(", ");
  Serial.println(returnVal.Z);
}

void loop() {}

hoek Function(int index)
{
  return Corners[index];
}

Same syntax as used for ints, C++ means no struct and no need for the C typedef.

a7