Struct and Arrow operator

Started by Zlatko Vid, December 27, 2021, 03:44:30 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Zlatko Vid

hello Charles

I tried to use arrow operator in o2..
i don't know if is supported ?

if not ( i don't see it in examples )
so i must use dot operator ..right ?

here is a C++ code :

// C++ program to show Arrow operator
// used in structure
#include <iostream>
using namespace std;

// Creating the structure
struct student {
    char name[80];
    int age;
    float percentage;
};

// Creating the structure object
struct student* emp = NULL;

// Driver code
int main()
{
   
    // Assigning memory to struct variable emp
    emp = (struct student*)
        malloc(sizeof(struct student));

    // Assigning value to age variable
    // of emp using arrow operator
    emp->age = 18;

    // Printing the assigned value to the variable
    cout <<" "<< emp->age;

    return 0;
}


and i made a mess here with o2 code :

' C++ program to show Arrow operator
' used in structure
'#include <iostream>
'using namespace std;
#lookahead

' Creating the structure
struct student
{
    char name[80]
    int age
    float percentage
}

' Creating the structure object
typedef struct {
      student* emp = int
      }

' Driver code
'sub main()

   
    ' Assigning memory to struct variable emp
    int emp = (student*)
       

    ' Assigning value to age variable
    ' of emp using arrow operator
    emp.age = 18

    ' Printing the assigned value to the variable
    print emp->age

    return 0

'end sub


' (sizeof(struct student));



Zlatko Vid

funny it looks that acting like TYPE
this one work

ps  ...so i guess "arrow" or "from" operator are not supported in o2
' Creating the structure
struct student
{
    char name[80]
    int age
    float percentage
}

' Creating the structure object
'typedef struct {
    '  emp = student* 
    '  }

' Driver code
'sub main()
*student emp
   
    ' Assigning memory to struct variable emp
   'int emp = student
       

    ' Assigning value to age variable
    ' of emp using arrow operator
    emp.age = 18
    emp.name = " John"

    ' Printing the assigned value to the variable
    print emp.age + emp.name

Nicola

#2
At this point, what is the difference, if any, between STRUCT and TYPE?


'Creating the structure
struct student
{
    char name[80]
    int age
    float percentage
}
type man
name as string
age as int
married as bool
end type

dim emp as student  ' Assigning memory to struct variable emp
       
' Assigning value to age variable
' of emp using arrow operator
  emp.age = 18
  emp.name = "John"

' Printing the assigned value to the variable
  print emp.age " " emp.name

man uo

uo.name="Marco"
uo.age=30
uo.married=false

print uo.name " " uo.married


Zlatko Vid

I am not sure
struct or structure is let say proper name for type

struct NOVA
{
int a
string b

}

NOVA p

p.a = 10
p.b = "ten"

Zlatko Vid

hmm is that even work:

uo.married=false

i mean FALSE

i am not sure i very rare use boolean ( -1) is  true , ( 0 ) is  false

Nicola

#5
No, true / TRUE is (1).  If you try to do:

...
uo.name = "Mark"
uo.age = 30
uo.married = true
print uo.name "" uo.married

the result is: Mark 1

This is strange since in the help it says that the value should be (-1) ... (why ????)

Again, in the help it says:

bool
USE:    specify a variable to hold Boolean true/false states
EXAMPLE:   bool t=true
                if not t then ...

REMARKS:    Notionally a Boolean type, but in reality. it is a 32bit signed integer, as in C

boolean
USE:    specify a variable to hold Boolean true/false states
EXAMPLE:   boolean t=true
                if not t then ...

REMARKS:    Notionally a Boolean type, but in reality. it is an sbyte (8 bit signed integer)

So, bool is 32 bit signed integer, boolean is 8 bit signed integer ---> where is the truth?



Zlatko Vid

#6
On windows Bollean true is  -1 and false is 0

in my case this works , note i use old A043 version
and i almost always using C style tying like INT a
when you make
member as INT you should use Dim

'Creating the structure o2 - A043
struct STUDENT
{
    char name[80]
    int age
    float percentage
}

type MAN
string name
int    age
married as bool
end type

STUDENT  emp ' Assigning variable emp as structure type STUDENT
       
' Assigning value to member age & member name

  emp.age = 18
  emp.name = "John"

' Printing the assigned value to the variable
  print emp.age  + " " + emp.name

MAN uo

uo.name = "Marco"
uo.age  = 30
uo.married = TRUE

print uo.name + " " +  uo.married



Zlatko Vid

for me this work too

type MAN
string name
int    age
married as bool
end type


hmmm  ;D

Charles Pegge

Both type and struct interpret their members the same way.

Pointer arrows are best avoided. '->' is translated to '.' in o2, and the pointering/indirection is resolved automatically from the struct.

Nicola

Thanks Charles.
Anyway, I did the test with version 030. Well, trying two versions you have different values:
Oxygen.dll,
version 0.2.3 2019-07-17T 18:12:07
TRUE = 1

version 0.3.0 2021-02-20T 13:31:46
TRUE = -1

Is this the latest version of Oxygen.dll?

Ciao

Charles Pegge

Yes 0.3.0 is the latest o2. Another minor update coming soon.