Rudy Gabardi
Rudy Gabardi

Reputation: 11

Segmentation Fault and other Warnings

Below is my code for a HW assignment in a C course I am taking. I am not sure where I've gone wrong but it asks for all the information but when it asks for what string to search it just core dumps. Below is the prompt we are given "Write a program to save student information in a struct array, information for each student includes id, GPA, address, phone number, and name. Provide a search function to return the student information with a certain last name input by a user."

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//a struct containing all the information of the students.
struct student{
int id;
float gpa;
char address;
char phone;
char name[];
};
// primary function
int main()
{
//the struct array.
struct student student[100];
//number of students
int n ;
printf("Enter the number of students:\n");
scanf("%d",&n);
//will request all of the information from the user
for(int i=0;i<n;i++)
{
printf("Enter the id of the student:\n");
scanf("%d",&(student[i].id));
printf("Enter the gpa of the student:\n");

scanf("%f",&(student[i].gpa));

printf("Enter the address of the student:\n");

scanf("%s",&(student[i].address));

printf("Enter the phone_no of the student:\n");

scanf("%s",&(student[i].phone));

printf("Enter the name of the student:\n");

scanf("%s",&(student[i].name));

}

//asks for what string to be searched.

printf("What string should be found?");

char student1[100];

scanf("%s", student1);

int found=-1 ;

//will find the index requested

for(int i=0;i<n;i++)

{

if(strcmp(student1[i], student))

{

found = i;

break;

}

}

if(found==-1)

{

printf("There is no record with the name");

}

//print the student's information

else

{

printf("The id of the student is %d\n",student[found].id);

printf("The gpa of the student is %f\n",student[found].gpa);

printf("The address of the student is %s\n",student[found].address);

printf("The phone_no of the student is %s\n",student[found].phone);

printf("The name of the student is %s\n",student[found].name);

}
return 0;
}

The other errors I am shown are

main.c:65:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[]’ [-Wformat=]                                                              
main.c:85:11: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]                                                                 
/usr/include/string.h:144:12: note: expected ‘const char *’ but argument is of type ‘char’                                                                                         
main.c:85:24: warning: passing argument 2 of ‘strcmp’ from incompatible pointer type [-Wincompatible-pointer-types]                                                                
/usr/include/string.h:144:12: note: expected ‘const char *’ but argument is of type ‘struct student *’                                                                             
main.c:115:40: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]                                                                   
main.c:117:41: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]

A display of how far I can get in my code before segmentation fault

Enter the phone_no of the student:                                                                                                                                                 
1                                                                                                                                                                                  
Enter the name of the student:                                                                                                                                                     
Steve                                                                                                                                                                              
What string should be found?steve                                                                                                                                                  
Segmentation fault                                                                                                                                                                 


...Program finished with exit code 139                                                                                                                                             
Press ENTER to exit console.  

Upvotes: 1

Views: 90

Answers (1)

NAND
NAND

Reputation: 687

You code should be like that:

In struct student:

You specified address and phone to be char which will be one character.

For char name[], you didn't specify what the size of the name should be.

In main :

According to this:

scanf Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments.

Since address, phone and name are the addresses of the array so you shouldn't use & operator because it will reutrn the address of the address.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//a struct containing all the information of the students.
struct student{
  int id;
  float gpa;
  char address[100];
  char phone[100];
  char name[100];
};
// primary function
int main()
{
  //the struct array.
  struct student student[100];
  //number of students
  int n ;
  printf("Enter the number of students:\n");
  scanf("%d",&n);
  //will request all of the information from the user
  for(int i=0;i<n;i++){
      printf("Enter the id of the student:\n");
      scanf("%d",&(student[i].id));
      printf("Enter the gpa of the student:\n");
      scanf("%f",&(student[i].gpa));
      printf("Enter the address of the student:\n");
      scanf("%s",(student[i].address));
      printf("Enter the phone_no of the student:\n");
      scanf("%s",(student[i].phone));
      printf("Enter the name of the student:\n");
      scanf("%s",(student[i].name));
  }
  //asks for what string to be searched.
  printf("What string should be found?");
  char student1[100];
  scanf("%s", student1);
  int found=-1 ;
  //will find the index requested
  for(int i=0;i<n;i++){
      if(!strcmp(student1, student[i].name)){
      found = i;
      break;
      }
  }
  if(found==-1)
      printf("There is no record with the name");  
  //print the student's information
  else {
      printf("The id of the student is %d\n",student[found].id);
      printf("The gpa of the student is %f\n",student[found].gpa);
      printf("The address of the student is %s\n",student[found].address);
      printf("The phone_no of the student is %s\n",student[found].phone);
      printf("The name of the student is %s\n",student[found].name);
    }
  return 0;
}

Upvotes: 1

Related Questions