Object Array Vocabulary Question

Hi,

I'm not sure where to look so I was hoping someone could help me understand why this code works...

What I am looking to understand is what the * and the & are and why they allow the code to compiles. TIA!

Code that doesn't compile

NexNumber n0 = NexNumber(2, 22, "n0"); //Set Temp Read out 1
NexNumber n1 = NexNumber(2, 23, "n1"); //Set Temp Read out 2
NexNumber n2 = NexNumber(2, 24, "n2"); //Set Temp Read out 3
NexNumber n3 = NexNumber(2, 25, "n3"); //Read Temp Read out 1
NexNumber n4 = NexNumber(2, 26, "n4"); //Read Temp Read out 2
NexNumber n5 = NexNumber(2, 27, "n5"); //Read Temp Read out 3

        NexNumber tempRead[3] = { n3, n4, n5 };
        NexNumber tempSet[3] = { n0, n1, n2 };

Code that Compiles

NexNumber n0 = NexNumber(2, 22, "n0"); //Set Temp Read out 1
NexNumber n1 = NexNumber(2, 23, "n1"); //Set Temp Read out 2
NexNumber n2 = NexNumber(2, 24, "n2"); //Set Temp Read out 3
NexNumber n3 = NexNumber(2, 25, "n3"); //Read Temp Read out 1
NexNumber n4 = NexNumber(2, 26, "n4"); //Read Temp Read out 2
NexNumber n5 = NexNumber(2, 27, "n5"); //Read Temp Read out 3

NexNumber *tempRead[3] = { &n3, &n4, &n5 };
NexNumber *tempSet[3] = { &n0, &n1, &n2 };

What is a NexNumber? Is it in a library? Where did you download it from?

That code does not compile. It is missing setup() and loop().

What is your actual question? What is this code supposed to do and how is that different to what it actually does?

The NexNumber is a object, it stands for the complete object. It is not a pointer. Just like the name of an 'int' or 'struct' are not pointers, they stand for the variable type as a whole.

In the first code you are creating new objects, but those objects are already created.
In the second code you are creating a list (an array) of pointers and fill it with the addresses of the objects. That makes sense 8)

TIA is Thanks In Advange ? Ouch, in my country a TIA is something very unpleasant.

@collectorcnc, this is for the Nextion displays. They come with a Arduino library that is not well written. Some Arduino users write their own code. I started to write easier and better functions, and at some point I was no longer using the functions of the library. For example to show a temperature, just a command to set text to "n0" is enough.

@MorganS - Sorry I'll be more clear next time I post.

@Koepel - Thanks for the quick response. I am very much having a difficult time understanding this nextion library! Just to make sure I understand,

This is a declaring an array of object NexNumber called tempRead that contains 3 NexNumber objects but it's creating an error because those objects have already been declared.

NexNumber tempRead[3] = { n3, n4, n5 };

I'm not sure I fully understand what the "*" does in this code. Am I correct in saying that the "&" is pointing to the previously declared objects?

NexNumber *tempRead[3] = { &n3, &n4, &n5 };

& is the "address of" operator. It gets the address of the object or variable. In order for a pointer to point at something, you need to know the address of the something.

  • is a little more complex. Due to historical conventions, when you declare a pointer, the * gets attached to the variable name when it's really part of the type.

NexNumber *p; creates a pointer (called p) to objects of type NexNumber. Note that it doesn't create any objects, just a place to store an arrow that you can later point towards any existing object.

[NexNumber* p; does exactly the same thing. But now it's more obvious that the type of p is NexNumber* rather than considering the * as a modifier to the variable, it's a modifier to the type definition.

1 Like

But now it's more obvious that the type of p is NexNumber* rather than considering the * as a modifier to the variable, it's a modifier to the type definition.

Where you run into problems putting the * with the type is with a statement like this:

  NexNumber* p, q;

p is a pointer to a NexNumber instance. q is NOT. q is a NexNumber object. To create two pointers, you need two *s:

  NexNumber *p, *q;

Putting the * with the type, in this case, just does not work.