Why doesn't my structure variable contain this member? - c

I wrote the following code:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int month;
int day;
int hour;
int minutes;
}primaries_date;
typedef struct
{
int all_members;
char *country;
primaries_date date;
}usa_primaries;
typedef struct node *ptr;
typedef struct node
{
usa_primaries up;
ptr next;
}Node;
void add(ptr usa_primaries *hptr, int all_members, char *con, int month, int day, int hour, int minutes)
{
ptr p;
ptr q;
ptr t;
t = malloc(sizeof(Node));
if(!t)
{
printf("Cannot build list");
exit(0);
}
t->all_members = members;
t->county = con;
t->date->month = d->month;
t->date->day = d->day;
t->date->hour = d->hour;
while( (p1) )
{
if( p->date->month >= month || p->date->day >= day || p->date->hour >= hour || p->date->minutes >= minutes )
{
q = p;
p = p->next;
}
}
if(p == *hptr)
{
*hptr = t; /*Resetting head. Assigning to the head t*/
t->next = p;
}
else
{
q->next = t;
t->next = p;
}
}
int main()
{
ptr h;
int month, day, hour, minutes;
int all_memebers; /*Declaration of all_members*/
char *county;
char member;
printf("Please enter the day");
scanf("%d",&day);
printf("Please enter the month");
scanf("%d",&month);
printf("Please enter the hour");
scanf("%d",&hour);
printf("Please enter the minutes");
scanf("%d",&minutes);
printf("Is this an all-member candidate? Y/N");
scanf("%c",&member);
if(member == 'Y')
all_members = 1;
else
all_members = 0;
printf("Please enter the country");
scanf("%s",&county);
add(&h,all_members,country,month,day,hour,minutes);
return 0;
}
I got this error:
usa.c: In function ���add���:
usa.c:42:6: error: ���struct node��� has no member named ���all_members���
t->all_members = members;
^
I don't really understand why this error occurred, since all_members is declared in the structure usa_primaries, and the structure node, contains the structure usa_primaries within it.
Why was this error displayed and how can I fix it?

Node doesn't have an all_members. It has a usa_primaries, and that has all_members. Therefore:
t->up.all_members

Related

Bus error while simply integer scanf sequence in C program

I have to take integer inputs and end the while loop if input is -1 in the following format in C:
111 1 1
111 1 1
111 1 1
-1
My code get bus error after 3rd scanf in the main function (end of the code). I know this is about white spaces but i have no idea to solve it. I am testing on MacBook with M1 if it is important. Btw print("break")s for just testing, i don't want to any junk print or scan.
#include <stdio.h>
#include <stdlib.h>
struct employee{
int ID;
int freeAt;
int totalTime;
struct employee *next;
};
struct customer{
int ID;
int startTime;
int processTime;
int waitingTime;
int helper;
struct customer *front;
struct customer *rear;
struct customer *next;
};
typedef struct employee employee;
typedef struct customer customer;
void new_employer(employee *top, int id){ //Insert employees by id
employee *ptr;
ptr = (employee*)malloc(sizeof(employee));
ptr->ID = id;
ptr->freeAt = 0;
ptr->totalTime = 0;
if (top == NULL){
ptr->next = NULL;
top = ptr;
}
else{
ptr->next = top;
top = ptr;
}
}
void help_customer(customer *c, employee *top){ //Match customer with suitable employee
employee *ptr;
ptr = (employee*)malloc(sizeof(employee));
ptr = top;
if (top==NULL){
c->startTime = c->startTime + 1;
c->waitingTime++;
help_customer(c,ptr);
}
if(c->startTime>=top->freeAt){
c->helper = top->ID;
top->freeAt = c->startTime + c->processTime;
top->totalTime = top->totalTime + c->processTime;
}
else{
help_customer(c, top->next);
}
}
void new_customer(customer *c, employee *top, int id, int start, int process){ //New customer
customer *ptr;
ptr = (customer*)malloc(sizeof(customer));
ptr->ID = id;
ptr->startTime = start;
ptr->processTime = process;
ptr->waitingTime = 0;
help_customer(ptr,top);
if (c->front == NULL){
c->front = ptr;
c->rear = ptr;
c->next = NULL;
}
else{
c->rear->next = ptr;
c->rear = ptr;
c->rear->next = NULL;
}
}
void customerStats(customer *c){ //Printing customer stats at the end of the transactions
customer *ptr;
ptr = (customer*)malloc(sizeof(customer));
ptr = c->front;
if(ptr == NULL){
printf("\nQUEUE IS EMPTY");
}
else{
printf("\n");
while (ptr!=c->rear){
printf("%d ", ptr->ID);
printf("%d ", ptr->helper);
printf("%d ", ptr->startTime);
printf("%d ", ptr->processTime);
printf("%d\n", ptr->waitingTime);
ptr = ptr->next;
}
printf("%d ", ptr->ID);
printf("%d ", ptr->helper);
printf("%d ", ptr->startTime);
printf("%d ", ptr->processTime);
printf("%d\n", ptr->waitingTime);
}
}
void employeeStats(employee *top){ //Printing employee stats at the end of the transactions
employee *ptr;
ptr = (employee*)malloc(sizeof(employee));
ptr = top;
if (top==NULL){
printf("\nSTACK IS EMPTY");
}
else{
while (ptr!=NULL){
printf("%d ", ptr->ID);
printf("%d\n", ptr->totalTime);
ptr = ptr->next;
}
}
}
int main(){
employee *e;
for(int i=1;i>7;i++){
new_employer(e,i);
}
customer *c;
int id;
int start;
int process;
while(1){ //********I GET BUS ERROR HERE ON 3RD SCANF**************
scanf("%d", &id);
printf("break1");
if (id==-1){
break;
}
scanf("%d", &start);
printf("break2");
scanf("%d", &process);
printf("break3");
new_customer(c,e,id,start,process);
}
customerStats(c);
employeeStats(e);
}
This is the terminal:
111
break11
break21
zsh: bus error ./main
It helps if you write minimal code to begin with, and get that working before adding more.
Here's a partial correction that shows why you are getting undefined behaviour (crashing). Read each line and understand why and how it differs from your code. Once that is understood, you can (cautiously, step-by-step) add one more element at a time and test, test, test as you build up toward your objective.
typedef struct employee {
int ID;
int freeAt;
int totalTime;
struct employee *next;
} employee_t;
employee_t *new_employeEEEE( employee_t *top, int id ) { //Insert employees by id
employee_t *ptr = (employee_t*)calloc( 1, sizeof(*ptr) ); // use calloc()
// Omitting check of calloc failing
ptr->ID = id;
ptr->next = top; // reverse sequence, but at least it works...
return ptr;
}
int main() {
employee_t *e = NULL;
// for( int i=1; i > 7;i++ ) !!!!
for( int i = 1; i < 7; i++ )
e = new_employeEEEE( e, i );
i = 1;
for( employee *p = e; p; p = p->next )
printf( "emp #%d has id: %d\n", i++, p->ID );
return 0;
}
Output:
emp #1 has id: 6
emp #2 has id: 5
emp #3 has id: 4
emp #4 has id: 3
emp #5 has id: 2
emp #6 has id: 1

C : find and remove a member of a struct of structs

I have two structs declared in this way:
struct record {
const char *entry;
struct record *next;
};
struct s_table {
int capacity;
struct record *records;
};
And I wrote a method to remove a member of the struct s_table:
int s_table_remove_record(struct s_table *t, const char *begin,
const char *end) {
if (!t) {
printf("No table");
return 0;
}
printf("inside remove_record \n");
if (strcmp(begin, t->records->entry) == 0) {
printf("record found in the first if.");
struct record *temp = t->records->next;
free(t);
return 1;
}
printf("Before allocating the head\n");
struct record *head = t->records;
for (; t->records->next; t->records = t->records->next) {
printf("Inside the for loop \n");
if (strcmp(begin, t->records->entry) == 0) {
printf("record found in the second if");
struct record *temp = t->records->next;
t->records->next = t->records->next->next;
free(temp);
return 1;
}
}
printf("record not found\n");
return 0;
}
However I never get inside the for loop. Could you help me understand why and provide a solution?
To test the code:
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct record {
const char *entry;
struct record *next;
};
struct s_table {
int capacity;
struct record *records;
};
static int counter = 0;
struct s_table *s_table_new() {
struct s_table *t = (struct s_table *)malloc(sizeof(t));
t->capacity = 1000;
t->records = (struct record *)malloc(sizeof(struct record) * t->capacity);
return t;
}
int s_table_add_record(struct s_table *t, const char *begin, const char *end) {
if (!t) {
printf("No table");
return 0;
}
if (counter > t->capacity) {
printf("Not enough space!");
return 0;
}
t->records[counter].entry = begin;
printf("Record add is: %s \n", t->records[counter].entry);
if (!t) {
printf("Failed to allocate new recrd");
return 0;
}
return 1;
}
int main() {
static const char *tomatoes = "tomatoes 10Kg CHF 55";
static const char *carrots = "carrots 15Kg CHF 40";
static const char *peppers = "peppers 2Kg CHF 6";
struct s_table *t = s_table_new();
assert(t);
s_table_delete(t);
struct s_table *t2 = s_table_new();
assert(t2);
t = s_table_new();
assert(t);
s_table_delete(t2);
s_table_add_record(t, carrots, carrots + strlen(carrots));
printf("First record added\n");
s_table_add_record(t, carrots, carrots + strlen(carrots));
printf("Second record added \n");
s_table_add_record(t, carrots, carrots + strlen(carrots));
s_table_add_record(t, carrots, carrots + strlen(carrots));
s_table_add_record(t, peppers, peppers + strlen(peppers));
s_table_add_record(t, tomatoes, tomatoes + strlen(tomatoes));
printf("All records added \n");
s_table_remove_record(t, peppers, peppers + strlen(peppers));
return 1;
}

Something wrong with add func

When I print the linked list countryName value always stay with the last string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct usa_primaries * ptr;
typedef struct primariesDate{
int month;
int day;
int hour;
}primariesDate;
typedef struct usa_primaries{
primariesDate date;
char *countryName;
int isOpen;
ptr next;
}usa_primaries;
void add (int month, int day, int hour, char *country, int isOpen, ptr *head) {
ptr t;
t = (ptr) malloc(sizeof(usa_primaries));
t->date.month = month;
t->date.day = day;
t->date.hour = hour;
t->countryName = (char *) malloc(strlen(country)+1);
strcpy(t->countryName, country);
t->isOpen = isOpen;
if(!(*head)) {
t->next = NULL;
*head = t;
}
else {
t->next = *head;
*head = t;
}
Below is the main function, I'm trying to print only countryName details but what I see is only the last value that is inserted. for example: scanf: test1, test2 the output is: test2 test2
int main() {
ptr head = NULL;
int month, day, hour, isopen;
char country[20];
while (scanf("%d %d %d %s %d", &month, &day, &hour, country, &isopen) != EOF) {
add(month, day, hour, country, isopen, &head);
}
ptr print = head;
while (print) {
printf("\n %s ", head->countryName);
print = print->next;
}
free(head);
return 0;
}
while (print) {
printf("\n %s ", head->countryName);
// ^^^^
print = print->next;
}
You print only the head in the loop. You should print the current node:
while (print) {
printf("\n %s ", print->countryName);
// ^^^^^
print = print->next;
}

I want to quick sort using linked lists

Write a function that takes two student record structures by reference and swaps all their contents except their next pointers. Use your functions to implement bubble sort algorithm to sort the linked list (do not use arrays).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*defined a structure for date of birth*/
struct birth{
int date;
int month;
int year;
};
struct studentrecord{
char name[64];
struct birth dob;
int height;
float weight;
struct studentrecord *next;
struct studentrecord *prev;
};
struct studentrecord* printlist(struct studentrecord* p){
if(p==NULL)
return 0;
printf("%s\t%2d%2d%4d\t%d\t%.2f\n",p->name,(p->dob).date,(p->dob).month,(p-
>dob).year,p->height,p->weight);
printlist(p->next);
}
/*swapped all the contents of 2 student records*/
void swap(struct studentrecord *c,struct studentrecord *d){
char temp[32];
strcpy(temp,c->name);
strcpy(c->name,d->name);
strcpy(d->name,temp);
int f;
f=(c->dob).date;
(c->dob).date=(d->dob).date;
(d->dob).date=f;
int m;
m=(c->dob).month;
(c->dob).month=(d->dob).month;
(d->dob).month=m;
int y;
y=(c->dob).year;
(c->dob).year=(d->dob).year;
(d->dob).year=y;
int h;
h=c->height;
c->height=d->height;
d->height=h;
float w;
w=c->weight;
c->weight=d->weight;
d->weight=w;
}
/*comparing the age of 2 students*/
int compareage(struct studentrecord *a, struct studentrecord *b){
if (a->dob.year<b->dob.year)
return 0;
else if(a->dob.year>b->dob.year)
return 1;
else if (a->dob.month<b->dob.month)
return 0;
else if(a->dob.month>b->dob.month)
return 1;
else if (a->dob.date<b->dob.date)
return 0;
else
return 1;
}
/*Here is where i think the problem starts*/
struct studentrecord *partition(struct studentrecord *l, struct
studentrecord *h)
{
// set pivot as h element
int x = h->dob.year*10000+h->dob.month*100+h->dob.date;
// similar to i = l-1 for array implementation
struct studentrecord *i = l->prev;
struct studentrecord *j ;
// Similar to "for (int j = l; j <= h- 1; j++)"
for (j= l; j != h; j = j->next)
{
if ((j->dob.year*10000+j->dob.month*100+j->dob.date)<= x)
{
// Similar to i++ for array
if(i == NULL)
i=l;
else
i=i->next;
swap(i, j);
}
}
if(i == NULL)
i=l;
else
i=i->next;
// Similar to i++
swap(i, h);
return i;
}
void quicksort(struct studentrecord *start,struct studentrecord *last){
if(last != NULL && start != last && start != last->next){
struct studentrecord *pi;
pi = partition(start,last);
quicksort(start,pi->prev);
quicksort(pi->next,last);
}
}
struct studentrecord *read(int n){
struct studentrecord *s;
if(n==0){
return NULL;
}
s=(struct studentrecord*)malloc(sizeof(struct studentrecord));
scanf("%s%d%d%d%d%f",s->name,&(s->dob.date),&(s->dob.month),&(s-
>dob.year),&s->height,&s->weight);
s->next=read(n-1);
return s;
}
int main(){
struct studentrecord *k, *temp1,*temp2;
int n, i;
printf("please enter the number of student records\n");
scanf("%d",&n);
k=read(n);
temp2=k;
k->prev=NULL;
while(k->next!=NULL){
temp1=k;
k=k->next;
k->prev=temp1;
}
temp1=k;
quicksort(temp2,temp1);
printlist(temp2);
}
I think there is some problem with my partition and quicksort functions(and hence the main function). I don't think i am doing it correctly and am getting a bit confused on how to sort the list. Any sort of help would be appreciated. Thanks!

I m trying to sort the node by score. I do not know what error i am having

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct student{
char firstname[20];
char lastname[20];
double grade;
char zipcode[10];
struct student *next;
};
void display(struct student *first)
{
while (first != NULL)
{
printf("\nFirst name: %s", first->firstname);
printf("\tLast name: %s", first->lastname);
printf("\tGrade: %.2f", first->grade);
printf("\t ZipCode: %s", first->zipcode);
first = first->next;
}
}
/*void add_Record(struct student *first)
{
char r[20];
struct student *t;
t = first;
while (t != NULL)
{
if (t == NULL)
{
first = (struct student*)malloc(sizeof(struct student));
printf("\nEnter the first name of the student:");
scanf("%s", first->firstname);
printf("\nEnter the last name of the student:");
scanf("%s", first->lastname);
printf("\nEnter the score of the student:");
scanf("%lf", &first->grade);
printf("\nEnter the zipcode of the student:");
scanf("%s", first->zipcode);
}
}
}
void del()
{
struct student *back, *t, *k;
char r[10];
int flag = 0;
printf("\nEnter the last name of student you want to delete:");
scanf("%s", r);
if (strcmpi(r, first->lastname) == 0)
{
first = first->next;
flag = 1;
}
else
{
back = first;
k = first->next;
while (k != NULL)
{
if (strcmpi(r, k->lastname) == 0)
{
back->next = k->next;
flag = 1;
break;
}
}
}
if (flag == 0)
printf("\nThe element not found!!!");
}
*/
void search(struct student *first)
{
char r[10];
int flag = 0;
printf("\nEnter the zipcode you want to search:");
scanf("%s", r);
struct student *t;
t = first;
while (t != NULL)
{
if (strcmp(r, t->zipcode) == 0)
{
printf("\nFirst name: %s", t->firstname);
printf("\tLast name: %s", t->lastname);
printf("\tGrade: %.2f", t->grade);
printf("\t ZipCode: %s", t->zipcode);
flag = 1;
break;
}t = t->next;
}
if (flag == 0)
printf("\nThe zipcode not in database!!");
}
void sort(struct student *first)
{
struct student *temp;
temp = NULL;
while (first != NULL)
{
if (first-> grade > first->next->grade)
{
strcpy(temp->firstname, first->firstname);
strcpy(temp->lastname, first->lastname);
temp->grade = first->grade;
strcpy(temp->zipcode, first->zipcode);
strcpy(first->firstname, first->next->firstname);
strcpy(first->lastname, first->next->lastname);
first->grade = first->next->grade;
strcpy(first->zipcode, first->next->zipcode);
strcpy(first->next->firstname, temp->firstname);
strcpy(first->next->lastname, temp->lastname);
first->next->grade = temp->grade;
strcpy(first->next->zipcode, temp->zipcode);
break;
}
}
printf("\nThe sorted record by score are: \n");
display(first);
}
int main(void)
{
struct student *first = NULL, *last = NULL;
struct student *temp;
int n;
printf("\nEnter the number of student:");
scanf("%d", &n);
int i;
for (i = 0; i < n; i++)
{
temp = (struct student*)malloc(sizeof(struct student));
printf("\nEnter the first name of the student:");
scanf("%s", temp->firstname);
printf("\nEnter the last name of the student:");
scanf("%s", temp->lastname);
printf("\nEnter the grade of the student:");
scanf("%lf", &temp->grade);
printf("\nEnter the zipcode of the student:");
scanf("%s", temp->zipcode);
temp->next = first;
first = temp;
}
int o;
o = 1;
while (o != 0)
{
printf("\nMENU\n");
printf("\nEnter 1 for displaying database.");
printf("\nEnter 2 for inserting an record.");
printf("\nEnter 3 for deleting a record by lastname.");
printf("\nEnter 4 for searching a record by zipcode.");
printf("\nEnter 5 for sorting record by score.");
printf("\nEnter 0 for exit!");
printf("\nEnter the choice:");
scanf("%d", &o);
switch (o)
{
case 1:display(first); break;
/*case 2:insertafter(*first); break;
case 3:del(); break;*/
case 4:search(first); break;
case 5: sort(first); break;
case 0:exit(0); break;
default:printf("\nYou have entered a wrong choice!!!");
}
}
}
The problem is that how do i sort the scores by score. My code seems right but doesn't work. I made a temp structure and tried to swap values but it doesn't work. Am i doing the loop wrong? Or all my code is wrong?
Here are two answers to the problem
First one sorts the nodes in the node linked list by bubble sorting them, rearranging the linked list
Second one walks the linked list, copies a pointer to each one into an array and then sorts the array by using the struct grade members, the linked list remains unchanged
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#define SWAP(T,x,y) {T *p = &(x); T *q = &(y); T z = *p; *p = *q; *q = z;}
struct student{
char firstname[20];
char lastname[20];
double grade;
char zipcode[10];
struct student *next;
};
struct student *head=NULL;
void add(char *f, char *s, double score) {
struct student *a;
a=(struct student*)malloc(sizeof(struct student));
strcpy(a->firstname,f);
strcpy(a->lastname,s);
a->grade = score;
a->next = head;
head = a;
}
void printout(char *label) {
struct student *node;
printf("%s\n",label);
for(node=head; node !=NULL; node=node->next) {
printf("%s %s %f\n", node->firstname, node->lastname, node->grade);
}
}
int main(int argc, char ** argv){
int mark=8;
struct student *walk, *prev;
add("Bob","Smith",10);
add("Eric","Von Däniken",90);
add("Morris","Minor",91);
add("Master","Bates",9);
add("Zoe","Bodkin",20);
add("Mary","Pippin",30);
/* bubble sort */
printout("before");
prev=head;
while(mark) {
mark=0;
for(walk=head;
walk != NULL;
walk=walk->next) {
if (walk->next && walk->grade > walk->next->grade) {
/* printf("swapping %s %s\n", walk->firstname, walk->next->firstname); */
mark=1;
if (walk == head) {
struct student *v2=walk->next;
struct student *v3=walk->next->next;
SWAP(struct student *, v3->next, v2->next);
SWAP(struct student *, head, v3->next);
walk = v3;
} else {
struct student *v1=prev;
struct student *v2=walk;
struct student *v3=walk->next;
SWAP(struct student *, v3->next, v2->next);
SWAP(struct student *, v1->next, v3->next);
walk = v3;
}
}
prev=walk;
}
}
printout("after");
return 0;
}
second version, uses qsort, retains linked list order
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
struct student{
char firstname[20];
char lastname[20];
double grade;
char zipcode[10];
struct student *next;
};
struct student *head=NULL;
size_t nodecount=0;
void add(char *f, char *s, double score) {
struct student *a;
a=(struct student*)malloc(sizeof(struct student));
strcpy(a->firstname,f);
strcpy(a->lastname,s);
a->grade = score;
a->next = head;
head = a;
nodecount++;
}
static int cmpgrade(const void *p1, const void *p2) {
struct student *g1, *g2;
g1=*(struct student **)p1;
g2=*(struct student **)p2;
return g1->grade > g2->grade;
}
int main(int argc, char ** argv){
int i;
struct student *walk,**sa, **sap;
add("Bob","Smith",10);
add("Eric","Von Däniken",90);
add("Morris","Minor",91);
add("Master","Bates",9);
add("Zoe","Bodkin",20);
add("Mary","Pippin",30);
/*copy into array of pointers*/
sa=calloc(sizeof (struct student*), nodecount);
sap=sa;
for(walk=head; walk != NULL; walk=walk->next) {
*sap = walk;
sap++;
}
printf("before\n");
for (i=0; i< nodecount; i++) {
printf("%s %s %f\n", sa[i]->firstname, sa[i]->lastname, sa[i]->grade);
}
/* qsort */
qsort(sa, nodecount, sizeof (struct student *), cmpgrade);
printf("after\n");
for (i=0; i< nodecount; i++) {
printf("%s %s %f\n", sa[i]->firstname, sa[i]->lastname, sa[i]->grade);
}
return 0;
}

Resources