Announcement

Collapse
No announcement yet.

How to implement c structure concept using 4DGL

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to implement c structure concept using 4DGL

    Hi,

    i am new to 4DGL programming. I want to use C 'structure' concept in 4DGL. Is there any built in data type for that.Any help will greatly be appreciated
    RHS

  • #2


    i am also looking for same. "C Structures Concept"

    Please confirm the release date.

    Comment


    • #3
      There's a few ways, here's an example of dynamic allocation which should give you ideas for 'cut down' implementations


      // demo showing simple dynamic structure encapsulation

      #constant MAXEMPLOYEES 100

      #constant NAME, PHONE, ZIP, WAGES, STRUCSIZE // enumerate the structure member offsets

      #constant NEW, FINDNAME, INDEX, DELETE, PRINT, EDIT // enumerate the functions methods

      // only 3 function methods shown for brevity
      func employee(var fnc, var data)

      // dynamic member array
      var private employees[MAXEMPLOYEES];
      var private count;

      // static instance of a member
      var private Name[20]; // 40 character name (string)
      var private Phone[10]; // 20 character phone number (string)
      var private Zip; // zip code (integer)
      var private Wage; // wage (integer)

      var *p;

      // methods
      if (fnc == NEW)
      if(count >= MAXEMPLOYEES)
      putstr("Error, too many employees\n");
      return 0;
      endif
      employees[count] := mem_Alloc(STRUCSIZE*2); // allocate memory for the structure
      p := employees[count++]; // get pointer to the allocation, advance index
      p[NAME] := mem_Alloc(strlen(Name) + 1); // allocate memory for the name
      to(p[NAME]); putstr(Name); // copy name to the allocation
      p[PHONE] := mem_Alloc(strlen(Phone) + 1); // allocate memory for the phone number string
      to(p[PHONE]); putstr(Phone); // copy phone number to the allocation
      p[ZIP] := Zip; // copy zip code (integer)
      p[WAGES] := Wage; // copy wage (integer)
      return 1; // return ok

      else if (fnc == INDEX)

      if(data > count)
      putstr("Error, bad index\n");
      return 0;
      endif
      p := employees[data-1]; // get pointer to the correct entry
      to(Name); putstr(p[NAME]); // copy the name
      to(Phone); putstr(p[PHONE]); // copy the phone number
      Zip := p[ZIP]; // copy zip code
      Wage := p[WAGES]; // copy wage
      return 1; // return ok

      else if (fnc == PRINT)

      if(data > count)
      putstr("Error, bad index\n");
      return 0;
      endif
      p := employees[data-1]; // get pointer to the correct entry
      print([STR] p[NAME],"\n"); // print the name
      print([STR] p[PHONE],"\n"); // print the phone number
      print(p[ZIP],"\n"); // print the zip code
      print(p[WAGES],"\n\n"); // print the wage
      return 1; // return ok

      else
      putstr("Method not yet implemented\n");
      return 0;
      endif


      endfunc


      func main()
      var *k;
      to(employee.Name); putstr("Fred Smith");
      to(employee.Phone); putstr("0456 992 335");
      employee.Zip := 1234;
      employee.Wage := 550;
      employee(NEW, 0); // add a new employee

      to(employee.Name); putstr("Jack Jones");
      to(employee.Phone); putstr("0123 446 129");
      employee.Zip := 5678;
      employee.Wage := 333;
      employee(NEW, 0); // add a new employee

      to(employee.Name); putstr("Andrew Green");
      to(employee.Phone); putstr("0555 124 678");
      employee.Zip := 9123;
      employee.Wage := 666;
      employee(NEW, 0); // add a new employee

      print(employee.count, " Employees\n\n");

      // direct access with a pointer
      k := employee.employees[1]; // get a pointer to the second entry
      putstr(k[NAME]);
      print("\n\n");

      // using a method to load the static instance
      employee(INDEX, 3);
      putstr(employee.Phone);
      print("\n\n");

      // using a method to print directly
      employee(PRINT, 1);
      employee(PRINT, 2);
      employee(PRINT, 3);


      repeat forever

      endfunc
      Regards,
      Dave

      Comment


      • #4


        Good one...thanks for ur effort...
        RHS

        Comment

        Working...
        X