Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
APIs from QB64PE and parameters defined As Any and unions of types ?
#6
An example of how unions and structures are used, and how much memory is used.

To explain again: In a union, the memory requirement is equal to the largest variable. All variables in a union have to share this memory, so one have to know exactly which one one want to use.
In a structure, each variable gets its own memory space.

Code: (Select All)

//union und struct Beispiel - 31. Mai 2024

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
union uni_beispiel
{
int i_zahl;
float f_zahl;
char c_zeichen;
}u_beispiel;

struct stu_beispiel
{
int i_stzahl;
float f_stzahl;
char c_stzeichen;
}st_beispiel;

u_beispiel.i_zahl = 12;
printf("\nUnion-Zahl: %3d", u_beispiel.i_zahl);

u_beispiel.f_zahl = 44.15;
printf("\nUnion-Fliesskommazahl: %5.2f", u_beispiel.f_zahl);

u_beispiel.c_zeichen = 'A';
printf("\nUnion-Zeichen: %c", u_beispiel.c_zeichen);

//Anzeigen wieviel Speicher union und struct belegen
printf("\n\nGroesse im Speicher von struct: %3d Bytes", sizeof(struct stu_beispiel));
printf("\nGroesse im Speicher der union : %3d Bytes", sizeof(union uni_beispiel));

return(0);
}

In both cases, access is done using dot notation, as with the type declaration in Basic.

Code: (Select All)

Dim Shared Motorrad As MotorradModell
"Modell : ", Motorrad.Modell

[Image: union-struct-Speicherverbrauch2024-05-31.jpg]
Reply


Messages In This Thread
RE: APIs from QB64PE and parameters defined As Any and unions of types ? - by Kernelpanic - 05-31-2024, 01:16 PM



Users browsing this thread: 11 Guest(s)