I’m brand new to programming..im just trying to make my own program to find the volume and surface area of spheres and cylinders. I cant figure out why this program keeps crashing before it will get to the rest of the code. I’m guessing the char* might be wrong but i cant see why it would be.
int main()
{
char* solid;
char* unit;
printf("Welcome to the Center of Spheres and Cylinders!\n");
printf("Would you like to look at a Sphere or a Cylinder?: ");
scanf("%s", solid);
if(solid == "Cylinder" || solid == "cylinder")
{
printf("You chose to look at a Cylinder.\n");
else if(solid == "Sphere" || solid == "sphere")
{
printf("You chose to look at a Sphere.\n");
it crashes just after I input for scanf...when i type in either cylinder or sphere it crashes. Thank you for the help
solidis a character pointer, it is not pointing to any allocated memory location which is causing your program crash when you try to read data into it withscanf()( and which is why it crashes right after that call as you observe).After you declare
you should
malloc()a certain amount of storage for it to point to. Alternatively you could have declared an array namedsolidNote that the crash is actually a good thing in that it is helpful to show there’s a problem with an errand pointer. This may not always happen unfortunately depending on where a pointer in memory is pointing too.