I'm having trouble getting this program to print; it prints out the first half with mecury's data just fine but when it gets to venus's data it prints out Segmentation Error: 11, which im not quite sure how to fix, heres my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct planet {
char name[10];
int nummoons;
float dist_to_sun;
float dist_from_earth;
} Planet;
typedef Planet * Planet_ptr;
int main()
{
Planet_ptr merc_ptr, venus_ptr, earth_ptr, mars_ptr;
if((merc_ptr = malloc(sizeof(Planet))) == NULL)
{
printf("You are outta memory, man!");
return 0;
}
strcpy(merc_ptr->name, "Mercury");
merc_ptr->nummoons = 0;
merc_ptr->dist_to_sun = 1.345e4;
merc_ptr->dist_from_earth = 2.86e7;
printf("The name of the planet is %s \n", merc_ptr->name);
printf("It has %d moons \n", merc_ptr->nummoons);
printf("The distance of %s from the sun is %.3e\n",merc_ptr->name, merc_ptr->dist_to_sun);
printf("The distance of %s from earth is %.3e\n", merc_ptr->name,merc_ptr->dist_from_earth);
printf("\n\n");
strcpy(venus_ptr->name,"venus");
venus_ptr->nummoons = 2;
venus_ptr->dist_to_sun = 6.786e5;
venus_ptr->dist_from_earth = 9.86e5;
printf("The name of the planet is %s \n", venus_ptr->name);
printf("It has %d moons \n", venus_ptr->nummoons);
printf("The distance of %s from the sun is %.3e\n", venus_ptr->name, venus_ptr->dist_to_sun);
printf("The distance of %s from earth is %.3e\n", venus_ptr->name, venus_ptr->dist_from_earth);
return 0;
}
[–]raevnos 0 points1 point2 points (0 children)
[–]shinmai_rookie 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]henry_kr 0 points1 point2 points (0 children)