Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I keep getting segmentation crashes I have an array of pointers that contaion st

ID: 3659884 • Letter: I

Question

I keep getting segmentation crashes I have an array of pointers that contaion structures in them like Array[0]->Str1->Str12->Str2->Str2. then, I'll pass the pointer array to a function where the user enters in numbers to be saved to the 5 float fields in Str1, and Str12. Then the mininum number will be saved in the first Str2 and the max will be saved in the Str2. I know i dont have all the code yet,
But it looks like I'm not allocating memory correctly because I keep on getting crashes/segmentation faults. Can anyone help out my jumbled mess of code and show me the correct way to do what I am trying to do? Let me know if you need any more explanations.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct S1{
float f1;
float f2;
float f3;
float f4;
float f5;
Str12 *next;
}Str1;


typedef struct S2{
float result;
struct S2 *next;
}Str2;

typedef struct S12{
float f1;
float f2;
float f3;
float f4;
float f5;
Str2 *next;
}Str12;



void getInput(Str1 **ptr);

main(){
Str1 **ptr;
Str1 *p;
Str12 *t;
Str2 *w;

ptr=(Str1**)calloc(21,sizeof(Str1*));
ptr[20]=NULL;
p=(Str1*)malloc(sizeof(Str1));
w=(Str2*)malloc(sizeof(Str2));
t=(Str12*)malloc(sizeof(Str12));
ptr[0]->next=p;
ptr[0]->next->next=t;
getInput(ptr);

}


void getInput(Str1 **ptr){
printf("Enter 5 numbers: ");
int n;
char temp[25];

printf("[1]Number: ");
fgets(temp, sizeof(temp), stdin);
n=strtod(temp,NULL);
ptr[0]->next->f1=n;

printf("[1]Number: ");
fgets(temp, sizeof(temp), stdin);
n=strtod(temp,NULL);
ptr[0]->next->f2=n;

printf("[1]Number: ");
fgets(temp, sizeof(temp), stdin);
n=strtod(temp,NULL);
ptr[0]->next->f3=n;

printf("[1]Number: ");
fgets(temp, sizeof(temp), stdin);
n=strtod(temp,NULL);
ptr[0]->next->f4=n;

printf("[1]Number: ");
fgets(temp, sizeof(temp), stdin);
n=strtod(temp,NULL);
ptr[0]->next->f5=n;
}

Explanation / Answer

// Here is the complete working code ... the changes are there in the comments .. u actually did not need double pointer **ptr .... the pointer *p would do ...plz go through the code .. run it .... u'll now get it :) #include #include #include typedef struct S2{ float min; // declare both fields for max and min results float max; }Str2; typedef struct S12{ float f1; float f2; float f3; float f4; float f5; Str2 *next; }Str12; //// define Str1 here typedef struct S1{ Str12 *next; }Str1; void getInput(Str1 *p); main(){ //Str1 **ptr; Str1 *p; Str12 *t; Str2 *w; int i; //ptr=(Str1**)calloc(21,sizeof(Str1*)); p=(Str1*)malloc(1*sizeof(Str1)); t=(Str12*)malloc(1*sizeof(Str12)); w=(Str2*)malloc(1*sizeof(Str2)); p->next=t; p->next->next=w; getInput(p); } void getInput(Str1 *p){ float minimum=0.0; float maximum=0.0; float n = 0.0; // declare n as float //char temp[25]; printf("Enter 5 numbers: "); // you can use simple scanf for taking the input printf(" [1]Number: "); //fgets(temp, sizeof(temp), stdin); //n=strtod(temp,NULL); scanf("%f",&n); minimum = n; maximum = n; p->next->f1 = n; printf(" [2]Number: "); //fgets(temp, sizeof(temp), stdin); //n=strtod(temp,NULL); scanf("%f",&n); if(minimum>=n)minimum=n; if(maximumnext->f2=n; printf(" [3]Number: "); //fgets(temp, sizeof(temp), stdin); //n=strtod(temp,NULL); scanf("%f",&n); if(minimum>=n)minimum=n; if(maximumnext->f3=n; printf(" [4]Number: "); //fgets(temp, sizeof(temp), stdin); //n=strtod(temp,NULL); scanf("%f",&n); if(minimum>=n)minimum=n; if(maximumnext->f4=n; printf(" [5]Number: "); //fgets(temp, sizeof(temp), stdin); //n=strtod(temp,NULL); scanf("%f",&n); if(minimum>=n)minimum=n; if(maximumnext->f5=n; p->next->next->min=minimum; p->next->next->max=maximum; printf(" The max : %f The min : %f ",(p->next->next->max),(p->next->next->min)); } //Hope it helps ... plz rate :)