rrz0

Struct Basics

Dec 7th, 2018
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* Struct basics
  6.  *
  7.  */
  8.  
  9. typedef struct {
  10.     char *name;
  11.     double diameter;
  12.     int moons;
  13.     double orbit_time, rotation_time;
  14. } planet_t;
  15.  
  16. void print_array(planet_t *current_planet, char *arr[]) {
  17.     strcpy(current_planet->name, "EARTH");
  18.     strcpy(arr[0], current_planet->name);
  19.     printf("From FUCN %s\n", arr[0]);
  20. }
  21.  
  22. void print_planet(planet_t *p1) {
  23.     printf("%s\n", p1->name);
  24.     printf(" Diameter: %.2f", p1->diameter);
  25.     printf(" Orbit Time: %.2f", p1->orbit_time);
  26. }
  27.  
  28. int main(int argc, char** argv) {
  29.  
  30.     //planet_t *current_planet = {"", 0, 0, 0, 0};
  31.    
  32.     char *arr[5];
  33.     char *firstPlanet = "Jupyter";
  34.     planet_t *p1;
  35.    
  36.     p1->name = malloc(strlen(firstPlanet) + 1);
  37.    
  38.     printf("HELLO");
  39.     //Assigning values to components
  40.     strcpy(p1->name, "Jupyter");
  41.     strcpy(arr[0], p1->name);
  42.     printf("From string %s\n", arr[0]);
  43.     p1->diameter = 142800;
  44.     p1->moons = 16;
  45.     p1->orbit_time = 11.9;
  46.     p1->rotation_time = 9.925;
  47.    
  48.    
  49.     print_planet(p1);
  50.    
  51.     print_array(p1, arr);
  52.    
  53.     return (EXIT_SUCCESS);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment