Star Struct

Star Struct

Next extend your program (stars.c) so that it can represent and print information on stars as a structure. Define a structure that can store the following information:

  • A numeric star identifier. Chose a basic type that can potentially represent all stars of an actual star databases, which may contain hundreds of millions of stars.
  • The name of the star in the form of a string. The string should be stored inside of the structure and hold names with up to 20 character symbols. Recall the representation of strings in C.
  • The name of the star's constellation, which are typically represented by 3-letter acronyms. Shorter acronyms may still occur. The constellation should also be stored inside the structure.
  • The star's distance from Earth in Parsecs as an integer number.
  • The star's magnitude. The magnitude is a logarithmic measure of the star's brightness. Chose an appropriate data type to store such values.

Chose a meaningful name for your the structure and use the typedef keyword to introduce a short name for the structure. Comment each member of the structure, i.e., explain its meaning, the expected value ranges, and units (if any).

Initializing Star Structs

Write a function that is able to initialize an existing star structure, i.e., that is stored either in a global variable with static storage duration, a local variable with static or automatic storage duration, or on the heap.

  • Chose a meaningful name for the function.
  • The function takes a pointer to a star structure as an argument and does not return any value (void).
  • Use the notation to access structure members through a pointer (->) seen in the class and initialize the structure members as follows:
    • All numeric values should be initialized to zero.
    • All string values should be initialized to the empty string. Attention, the strings are stored as arrays. So think about what it means to have an empty string in an array? Ask a TA if you have doubts. A safe option is to initialize all array elements using a for loop.

Next write a function to print the information stored in a star struct to a file:

  • Chose a meaningful name for the function.
  • The function should take a pointer to a FILE structure and a constant pointer to a star as inputs (const).
  • The function is not supposed to return a value.
  • Use the fprintf function to display the information as follows:
    • First print the numeric ID. Make sure that the number are printed with a minimal width of 6 characters and aligned to the right.
    • Then print the star's constellation. All constellations should be printed with a minimal width of 3 and aligned to the right.
    • Then print the distance of the star with a width of 3.
    • Next, print the star's magnitude. Make sure that the magnitude's sign is always printed (for both positive and negative values). Print only 2 digits after the decimal point and make sure that the whole number along with its sign is printed with a total width of 5.
    • Finally, print the star's name.
    • The star information should be printed on a single line ending with a line feed ('\n'). Fields should be separated by a single space (' ').
  • Check the return value of fprintf for errors. If an error is signaled, print an error message using perror and end the program by calling the function exit as explained in the lecture.

Lookup the usage of fprintf and its format strings in the lecture slides, the book, or on the cppreference website.

Test

Write a test function (with an appropriate name) to test the two functions from above, i.e., by printing the information stored in a star structure after initialization. Also test printing star structures that you initialize manually using the {} notation for structures, e.g., using the initializer {30365,"Canopus","Car", 94,-0.62}.

Call the test function from your main function. Make sure that the entire code compiles, runs, and behaves as expected.