data structures - How to implement retrieve and update in a C struct -


Suppose we have the following structure in C:

  ; Four names [50]; Four nickname [50]; Four courses [50]; };   

Any person can retrieve (R, C, S) and updates (R, C, S) .

  • Retrieve () R a
    Structure student , s An indicator for this would be the value of this field in the area of ​​ struct and C .
  • Update () then Structure student , an indication for s Struct is a field of and finally the value of c in the field struct . Since you are limited to using C, your options are limited.

    In fact, there are two different options (assuming you do not want to dive into memory / pointers magic):

    • Your data on the basis of the actual type Create alternate functions to set / retrieve
    • Create your own "version" datatype as a struct .

      Using the Dispute Hire Function, you can do something like this:

        Recover vowel intent (Constant Student Student Dataset, Const char * Field, int * value) {ifet! (! Dataet ||! Field ||! Value) // some minor error checking return; If (! Strcmp (field, "AM")) {* value = dataet-> AM; Return; } Printf ("tried to recover unknown integer field '% s'! \ N", field); }   

      Receiving stars or setting integers will work in the same way by using their jobs.

      Using custom version datatype:

        structure type {char type; Union {four strings [50]; Integer integer; }; }; Recover empty (Constructor Student * Dataset, Constant * Field, Straight Variient * Value) (If (! Dataset ||! Field ||! Value) // Some minor error check returns; If (! Strcmp (field, "AM" ); {Value-> gt; 0; // Suppose it represents an integer value value-> integer = dataset-> gt; return;} printf ("Unknown integer field '% s' Tried to recover! \ N ", field);} int varToInt (in struct version * Not) {if (value- & gt; type == 0) return Value- & gt; integer; Else // 0 not an integer value return;}    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -