CIS3355:
Business Data Structures |
How do we declare a struct in other languages? How do we declare them in c?
Before entering into matter, we should begin by defining what a STRUCT is. A STRUCT is basically a new data type which can be manipulated in the same matter as any of the other basic data types (int, float, char, etc.) has the same properties as an array meaning that:
WITH THE EXEPTION OF ONE OF THE PROPERTIES OF AN ARRAY
As a matter of fact, a STRUCT could be made of two or more data types. One common example, and perhaps the easiest to understand and explain, are DATABASES.
In this example, the following data types were used.
The preceding information raises one question
How many and what data types may be used in a STRUCT There is no best way to combine the data types, they can be combined to suit the needs of the programmer. As far as how many data types can be used the topic is still highly debatable, consider the following table as the range of possible combinations.
To declare a STRUCT in another language one must obey the language’s instructions. For instance:
COBOL
01 Customer Info 02 Name PIC X(40). 02 SSN PIC 9(9). 02 DOB PIC 9(6). 02 Address PIC X(35). 02 Occupation PIC X(30). 02 Age PIC 9(3).
For instance, in COBOL the X or 9 in after PIC identifies the code on the specific field. For instance, 9 is numeric and X is Alpha-Numeric
PASCAL
Type Customer Info = record Name : array [1..40] char SSN : interger; DOB : integer; Adress :array[1..35] of char; Occupation : array [1..30]; Age : integer; end;
In PASCAL, the procedure is similar to c. The type of data to be used is declared after the title of the field.
These are just a couple of examples; the code will vary in accordance with the programming scheme of the language used.
When using c, we basically use a compilation of previously used data types and generate a new and distinct data type. The steps to declare a STRUCT in c are as follows
1 Continuing with the database example, we begin by determining what fields we wish to have in our record. 2 We then proceed to determine the data type for each to be used for each of the fields. 3 We then determine the amount of bytes to be assigned to each of the fields.
4 We conclude bye declaring the RECORD in c with accordance with the information previously selected.
struct Customer Info { char Name [31]; char SSN [10]; char DOB [5]; char Address [41]; int age [2]; };
int main() { struct Custmer Info active = {"Favre, Brett",12345,"CIS",3.27,67}; struct student alumni = {"White, Reggie", 23456, "Pain", 1.34, 89};
printf("Name: %13s %14s\n",active.student_name, alumni.student_name); printf("ID: %13d %14d\n",active.student_id, alumni.student_id); printf("Major: %13s %14s\n",active.major, alumni.major); printf("gpa: %13.3f %14.3f\n",active.gpa, alumni.gpa); printf("Hours: %13d %14d\n",active.total_hrs, alumni.total_hrs); return 0;}
*Why is there an extra byte allocated for Name, SSN, DOB, Address, Occupation, and Age if they should consist of 30, 10, 5, 40, and 30 correspondingly. The reason for this, is because we are storing these fields as the data type character array, which means that we need to contemplate one extra byte for the null character. If we do not do this, we would have to keep track of the length of each of the strings.
How many total Bytes are needed for a STRUCT ?
a) 128 non-contiguous bytes. b) 128 contiguous bytes. c) 64 contiguous bytes. d) less than 64 bytes.
ANSWER: B
What is the primary difference between a STRUCT and an array? a) There is no difference they are the same. b) A Struct can only have one data type. c) A Struct can have several data types. d) A array can have does not need contiguous space.
ANSWER: C
OTHER GOOD REFERENCES |