I've tried malloc, and no malloc and it will build but not run or compile. When I run the code on codepad.org it gives me a segmentation error. I have an array of structures I'm inputting and I'm searching through them for a specific item. That's as far as I got and no compile. The code is as follows (I used netbeans, codeblocks, and visual basic 2012 programs):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 20
#define BLOODTYPESIZE 4
#define MAX 120000
typedef struct {
int month;
int day;
int year;
} dateT;
typedef struct {
int hour;
int minute;
} timeT;
typedef struct {
char name[SIZE];
char organname[SIZE];
char bloodtype[BLOODTYPESIZE];
dateT dateAdded;
timeT timeAdded;
int received;
} organT;
int main(void) {
int i, n, k, j;
int c;
int *ptr;
char organ[SIZE];
char bloodkind[BLOODTYPESIZE];
organT patient[MAX];
scanf("%d",&n);
ptr = (int *)malloc(n * sizeof(*ptr));
printf("Enter patient information\n");
for(i=1; i<=n; i++){
scanf("%s", patient[i].name[SIZE]);
scanf("%s", patient[i].organname[SIZE]);
scanf("%s", patient[i].bloodtype[BLOODTYPESIZE]);
scanf("%d %d %d", patient[i].dateAdded);
scanf("%d %d", patient[i].timeAdded);
patient[i].received = 0;
}
scanf("%d", &k);
for(j=0; j<k; j++) {
gets(organ);
printf("Organ received: %s", organ);
gets(bloodkind);
printf("Organ has blood type: %s", bloodkind);
}
for (c=0; c<n; c++){
if(patient[i].organname == organ){
if(patient[i].bloodtype == bloodkind){
if(patient[i].received == 0) {
printf("Patient(s) Found!\n");
printf("%s", patient[i].name[SIZE]);
printf("Organ received: %s", organ);
patient[i].received = 1;
}
if(patient[i].received == 1)
printf("Patient already received organ\n");
}
else("Not correct blood type\n");
}
else("No match found\n");
}
return (EXIT_SUCCESS);
}
Looks like you are not using the address correctly. For example, when you say
scanf("%s", &patient[i].name[SIZE]);
you are actually reading past the allocated space for patient[i].name. You should change the statement to
scanf("%s",patient[i].name);
and fix other statements similarly.
First check n compare to MAX
here you access to bloodtype[BLOODTYPESIZE] but the last item in this tab is bloodtype[BLOODTYPESIZE-1]
scanf("%s", &patient[i].bloodtype[BLOODTYPESIZE]);
The same problem is tru for other acces in the pararagraph.
Related
I have a noticed a strange bug in my short code. The purpose of this script is to create an array of structures that contain some information about cars
("Struct Car Car1: char brand[50], char model[50], int ccm, int hp, int kw, char[50]")
After running the script everything is fine up until the first instance is filled, but when the variable i has been incremented by 1, the line break disappears from between the lines where I ask the user to provide the "BRAND" and the "MODEL". I am sorry for being a noob this is my first time using structs. Thanks for any kind of help.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define _CRT_SECURE_NO_WARNINGS
#define MAX 10
int main() {
struct Car {
char brand[50];
char model[50];
int ccm;
int hp;
int kw;
char wheel[50];
};
struct Car Car1;
strcpy(Car1.brand, "Skoda");
strcpy(Car1.model, "Octavia");
Car1.ccm = 1960;
Car1.hp = 170;
Car1.kw = 114;
strcpy(Car1.wheel, "215/75R18");
printf("The car's brand is: %s\n", Car1.brand);
printf("The model is: %s\n", Car1.model);
printf("The volume of the engine is %d ccm\n", Car1.ccm);
printf("It has %d horsepower\n", Car1.hp);
printf("The car has %d kws\n", Car1.kw);
printf("You can put: %s wheels on it\n", Car1.wheel);
struct Car car_arr[MAX];
int i;
for (i = 0; i < MAX; i++) {
printf("Enter details of a car: %d\n", i+1);
printf("\n");
printf("Enter the brand: ");
fgets(car_arr[i].brand, sizeof car_arr[i].brand, stdin); //Brand
car_arr[i].brand[strcspn(car_arr[i].brand, "\n")] = '\0';
printf("Enter the model: ");
fgets(car_arr[i].model, sizeof car_arr[i].model, stdin); //Model
car_arr[i].model[strcspn(car_arr[i].model, "\n")] = '\0';
printf("Enter the parameters of the wheel: ");
fgets(car_arr[i].wheel, sizeof car_arr[i].wheel, stdin); //Wheel
car_arr[i].wheel[strcspn(car_arr[i].wheel, "\n")] = '\0';
printf("Enter the volume of the engine in ccms: ");
scanf_s("%d", &car_arr[i].ccm);
printf("Enter the horsepower: ");
scanf_s("%d", &car_arr[i].hp);
printf("Enter the amount of kilowatts: ");
scanf_s("%d", &car_arr[i].kw);
}
return 0;
}
Tha above comments answered your question, but as a side note:
You can save yourself some repetition by using "typedef" together with "struct".
typedef struct { int x1; long x2; double etc; } my_type;
Hello i'm trying to build a function to search a car for a client by client demand.
The structure contains: model,year,price.
the client is asked to enter his demands and then the code calls a function that check if there is a car in the structure that is suitable for him.
I get error for "access violation reading error"
thanks!
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define SIZE 10
typedef struct
{
char model[10];
float price;
int year;
}car;
void findCar(car *arr[], int minYear, float maxPrice, char modelWanted, int carAmount);
int main()
{
int carAmount;
car* arr;
puts("How many cars?");
scanf("%d", &carAmount);
arr = (car*)malloc(carAmount * sizeof(car));
if (arr == NULL)
return -1;
for (int i = 0; i < carAmount; i++)
{
puts("Enter car details, Model, Price,Year");
scanf("%s%f%d",arr[i].model,&arr[i].price,&arr[i].year);
}
char modelWanted[SIZE];
float maxPrice;
int minYear;
puts("Enter wanted model,maximum price and minimum year!");
scanf("%s%f%d", modelWanted, &maxPrice, &minYear);
for (int i = 0; i < carAmount; i++)
printf("Model is: %s, Price is: %.2f, Year is: %d\n", arr[i].model, arr[i].price, arr[i].year);
findCar(&arr, minYear, maxPrice, modelWanted, carAmount);
free(arr);
return 1;
}
void findCar(car *arr[], int minYear, float maxPrice, char modelWanted,int carAmount)
{
int i, counter = 0;
for (i = 0; i < carAmount; i++)
if (((strcmp(arr[i]->model, modelWanted)) == 0) && (arr[i]->year >= minYear) && (arr[i]->price <= maxPrice))
{
printf("Model is: %s, Price is: %.2f, Year is: %d\n", arr[i]->model, arr[i]->price, arr[i]->year);
++counter;
}
printf("We found %d cars for you!", counter);
}
You are passing pointer to array of struct
car *arr[]
so instead of accessing elements by arr[i]->model like you do, you should access them using (*arr)[i].model. The method you uses is for accessing the array of pointers to struct element, but you have pointer to array of struct.
Of course already commented char instead of char* will also cause run time error, but you should have received compiler warning for this.
I have to declare a vector with the "struct" type which, for every n students, it creates a value for the group that student belongs to (which is like a counter), their names and their grades.
The program has to output the name of the students with the highest grade found in these groups. I have to allocate the vector on the heap (I only know the theoretical explanation for heap, but I have no idea how to apply it) and I have to go through the vector using pointers.
For example if I give n the value 4, there will be 4 students and the program will output the maximum grade together with their names as shown here.
This will output Ana 10 and Eva 10.
I gave it a try, but I have no idea how to expand it or fix it so I appreciate all the help I can get with explanations if possible on the practical application of heap and pointers in this type of problem.
#include <stdio.h>
#include <stdlib.h>
struct students {
int group;
char name[20];
int grade;
};
int main()
{
int v[100], n, i;
scanf("%d", n);
for (i = 0; i < n; i++) {
v[i].group = i;
scanf("%s", v[i].name);
scanf("%d", v[i].grade);
}
for (i = 0; i < n; i++) {
printf("%d", v[i].group);
printf("%s", v[i].name);
printf("%d", v[i].grade);
}
return 0;
}
Here I was just trying to create the vector, nothing works though..
It appears, int v[100]; is not quite what you want. Remove that.
You can follow either of two ways.
Use a VLA. After scanning the value of n from user, define the array like struct students v[n]; and carry on.
Define a fixed size array, like struct students v[100];, and use the size to limit the loop conditions.
That said,
scanf("%d", n); should be scanf("%d", &n);, as %d expects a pointer to integer type argument for scanf(). Same goes for other cases, too.
scanf("%s", v[i].name); should better be scanf("%19s", v[i].name); to avoid the possibility of buffer overflow by overly-long inputs.
Even though you are asking for the number of students (groups) using scanf, you hardcoded the upper bound of this value using v[100]. So, I passed your input variable n (the number of students) to malloc in order to allocate the space you need for creating an array of n students.
Also, I used qsort to sort the input array v where the last element would be the max value. Here qsort accepts an array of structs and deference the pointers passed to the comp function to calculate the difference of the comparison.
Finally, I printed the sorted array of structs in the last loop.
#include <stdio.h>
#include <stdlib.h>
struct students {
int group;
char name[20];
int grade;
};
int comp(const void *a, const void *b)
{
return ((((struct students *)a)->grade > ((struct students *)b)->grade) -
(((struct students *)a)->grade < ((struct students *)b)->grade));
}
int main()
{
int n;
printf("Enter number of groups: ");
scanf("%d", &n);
printf("\n");
struct students *v = malloc(n * sizeof(struct students));
int i;
for(i = 0; i < n; i++)
{
v[i].group = i;
printf("\nName: ");
scanf("%s", v[i].name);
printf("Grade: ");
scanf("%d", &v[i].grade);
}
qsort(v, n, sizeof(*v), comp);
for(i = 0; i < n; i++)
{
printf("Group %d, Name %s, grade %d\n", v[i].group, v[i].name, v[i].grade);
}
return (0);
}
You need to replace the standalone array v[100], with an array of structs referencing your structure:
struct students v[100];
However, if you want to use malloc to allocate memory on the heap, you will need to do something like this:
struct students *students = malloc(n * sizeof(struct students));
/* Check void* return pointer from malloc(), just to be safe */
if (students == NULL) {
/* Exit program */
}
and free the requested memory from malloc() at the end, like this:
free(students);
students = NULL;
Additionally, adding to #Sourav Ghosh's answer, it is also good to check the return value of scanf(), especially when dealing with integers.
Instead of simply:
scanf("%d", &n);
A more safe way is this:
if (scanf("%d", &n) != 1) {
/* Exit program */
}
With all this said, your program could look something like this:
#include <stdio.h>
#include <stdlib.h>
#define NAMESTRLEN 20
typedef struct { /* you can use typedef to avoid writing 'struct student' everywhere */
int group;
char name[NAMESTRLEN+1];
int grade;
} student_t;
int
main(void) {
int n, i;
printf("Enter number of students: ");
if (scanf("%d", &n) != 1) {
printf("Invalid input.\n");
exit(EXIT_FAILURE);
}
student_t *students = malloc(n * sizeof(*students));
if (!students) {
printf("Cannot allocate memory for %d structs.\n", n);
exit(EXIT_FAILURE);
}
for (i = 0; i < n; i++) {
students[i].group = i;
printf("Enter student name: ");
scanf("%20s", students[i].name);
printf("Enter students grade: ");
if (scanf("%d", &(students[i].grade)) != 1) {
printf("Invalid grade entered.\n");
exit(EXIT_FAILURE);
}
}
printf("\nStudent Information:\n");
for (i = 0; i < n; i++) {
printf("Group: %d Name: %s Grade: %d\n",
students[i].group,
students[i].name,
students[i].grade);
}
free(students);
students = NULL;
return 0;
}
I am not getting the correct output here,
The code takes the no of inputs and option as an input, then takes the name of student year and gender and based on option provided gives the output. The output can be the name appearing first in the dictionary or the smaller value of the year amongst the inputs.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student_record
{
int passing_year;
char gender;
char name[20];
};
typedef struct student_record student;
student* find_specific(student* find, int option_num, int number)
{
int i;
student* temp = find;
int opt = option_num;
int num = number;
if(opt==1)
{
for(i=0; i<num; i++)
{
if( strcmp(temp->name, find[i].name) >0)
temp = find+i;
}
}
else
{
for(i=0; i<num; i++)
{
if (temp->passing_year > find[i].passing_year)
temp = find+i;
}
}
return temp;
}
int main() {
student* example;
student* final;
int i;
int option_num, number_of_students;
printf("Enter 2the number of students, option number");
scanf("%d" "%d", &number_of_students, &option_num);
example = (student* )malloc(number_of_students * sizeof(student));
printf("Enter the name, passing year and gender");
for(i=0; i< number_of_students; i++)
{
scanf("%s" "%d" "%c", example[i].name, &example[i].passing_year, &example[i].gender);
}
final = find_specific(example, option_num, number_of_students);
printf("%s" "%d" "%c", final->name, final->passing_year, final->gender );
return 0;
}
i am getting a segmentation fault. I can't figure out exactly where am I screwing up.
Your scanf() and printf() format strings are probably wrong.
scanf("%s" "%d" "%c", example[i].name, &example[i].passing_year, &example[i].gender);
should probably be
scanf("%s %d %c", example[i].name, &example[i].passing_year, &example[i].gender);
(without the extra quotes). The compiler will concatenate adjacent
string literals, so instead of a compiler error, it interpreted your format string as equivalent to "%s%d%c" (without whitespace in between).
That probably doesn't match the layout of your input, so some of the values were probably left uninitialized in a way that caused problems later.
You should always check the return value of scanf and similar library functions,
to ensure that you got the input format you told the compiler to expect.
The question:
Read up to 6 pairs of names and ages into TWO separate arrays, and use a linear search to locate a target name and to print that person’s age. The two arrays are called names and ages:
I am getting lots of errors .. I am not sure about passing the arrays into functions..
#include <stdio.h>
#define ASIZE 20
#define RECSIZE 6
struct record {
char name[ASIZE];
int age[ASIZE];
};
struct record na[RECSIZE];
int linearSearch(struct record *a, char *find)
{
int x;
for(x=0; x<RECSIZE; x++)
{
// if(na[x].name==find[x])
if(a->name[x]==find[x])
{
return x;
}
}
return -1;
}
int main()
{
int i;
for (i=0; i<RECSIZE; i++)
{
printf("Enter name: ");
scanf("%s",na[i].name);
printf("Enter age: ");
scanf("%i",&na[i].age);
}
printf("Enter the Search name: ");
char temp[ASIZE];
scanf("%s",temp[ASIZE]);
int result;
result=linearSearch(&na, &temp[]);
printf("%i", result);
return 0;
}
Please help.
The error is in:
result=linearSearch(&na, &temp[]);
#include <stdio.h>
#include <string.h>
#define ASIZE 20
#define RECSIZE 6
struct record {
char name[ASIZE];
int age;
};
struct record na[RECSIZE];
int linearSearch(struct record *a, char *find){
int x;
for(x=0; x<RECSIZE; x++){
if(strcmp(a[x].name, find)==0)
return x;
}
return -1;
}
int main(){
int i;
for (i=0; i<RECSIZE; i++){
printf("Enter name: ");
scanf("%s", na[i].name);//No protection when entered past the buffer
printf("Enter age: ");
scanf("%i", &na[i].age);
}
printf("Enter the Search name: ");
char temp[ASIZE];
scanf("%s", temp);
int result;
result=linearSearch(na, temp);
printf("%i", result);
return 0;
}
On my system, the compiler gave me these errors for your exact code. Just look at the line numbers (should be exactly like yours) and address each error by looking at the error description. Once you address these, you will be further down the path of understanding your execution flow: