C: Manipulation with structures

In summary, the code compiles successfully but doesn't give the output:#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct{ char surname[101]; char name [101];}AUTHOR;typedef struct{ char title[20]; int year; int number_of_authors; AUTHOR *author;}BOOK;void read_author(AUTHOR *pa)//reads data about an author{ printf("surname:"); scanf("%s",pa
  • #1
gruba
206
1

Homework Statement


Write a program that reads data about n number of books.
Sort data about books lexicographically by title (if the title is the same, sort by publish year), and then for every book sort data about authors.
Then, read name and surname of one of the previously read authors, and print data about all books which are from that one author.

Homework Equations


3. The Attempt at a Solution [/B]
The following code compiles successfully but doesn't give the output:
Code:
#include <stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct
{
  char surname[101];
  char name [101];
}AUTHOR;

typedef struct
{
  char title[20];
  int year;
  int number_of_authors;
  AUTHOR *author;
}BOOK;

void read_author(AUTHOR *pa)//reads data about an author
{
  printf("surname:");
  scanf("%s",pa->surname);
  printf("name:");
  scanf("%s",pa->name);

}

void read_book(BOOK *pb)//reads data about a book
{
  printf("title:");
  scanf("%s",pb->title);
  printf("publish year:");
  scanf("%d",&pb->year);
  printf("number of authors:");
  scanf("%d",&pb->number_of_authors);
  pb->author=calloc(pb->number_of_authors,sizeof(*pb->author));
  int i;
  for(i=0;i<pb->number_of_authors;i++)
  {
  printf("%d. authors:\n",i+1);
  read_author(pb->author+i);
  }
}

int compare_authors(AUTHOR *a,AUTHOR *b)//compares authors by surname (for sorting)
{
  int i;
  for(i=0;a->surname[i] && a->surname[i] == b->surname[i];i++);
  return a->surname[i]-b->surname[i];
}

void sort_authors(AUTHOR *arr,int n)//sorts authors lexicographically
{
  int i,j;
  for(i=0;i<n-1;i++)
  for(j=i+1;j<n;j++)
  if(compare_authors(arr+i,arr+j)>0)
  {
  AUTHOR temp=arr[i];
  arr[i]=arr[j];
  arr[j]=temp;
  }
}

int compare_books(BOOK *a,BOOK *b)//compares books by title (for sorting)
{
  int i;
  for(i=0;a->title[i] && a->title[i] == b->title[i];i++);
  return a->title[i]-b->title[i];
}

void sort_books(BOOK *arr,int n)//sorts books lexicographically
{
  int i,j;
  for(i=0;i<n-1;i++)
  for(j=i+1;j<n;j++)
  {
  if(compare_books(arr+i,arr+j)>0)
  {
  BOOK temp=arr[i];
  arr[i]=arr[j];
  arr[j]=temp;
  }
  else
  if((compare_books(arr+i,arr+j) == 0) )
  {
  BOOK temp=arr[i];
  arr[i]=arr[j];
  arr[j]=temp;
  }

  }
}

void print(BOOK *pb,int n,char psurname[101],char pname[101])//prints data about books of read author (if exists)
{
  int i,j;
  scanf("%s",psurname);
  scanf("%s",pname);
  for(i=0;i<n;i++)
  {
  for(j=0;j<pb->number_of_authors;j++)
  {
  if((strcmp(pb->author[j].surname,psurname)== 0) &&
  (strcmp(pb->author[j].name,pname)==0))
  printf("%s %d %d %s %s",pb->title,pb->year,pb->number_of_authors,
  pb->author[j].surname,pb->author[j].name);
  }
  }
}

void dealloc(BOOK *pb)// free allocated memory
{
  free(pb->author);
}int main()
{
  int i,n;
  BOOK *arr;
  char psurname[101],pname[101];
  do
  {
  printf("n=");
  scanf("%d",&n);
  }
  while(n<1);
  arr=(BOOK *)malloc(n*sizeof(BOOK));
  printf("enter books:\n");
  for(i=0;i<n;i++)
  {
  printf("%d. book:\n",i+1);
  read_book(arr+i);
  }
  sort_books(arr,n);
  for(i=0;i<n;i++)
  {
  printf("%d.",i+1);
  print(arr+i,n,&psurname,&pname);
  dealloc(arr+i);
  }
  free(arr);
  return 0;
}

Compiler gives the following warnings:
warning: passing argument 3 of 'print' from incompatible pointer type [enabled by default]|
note: expected 'char *' but argument is of type 'char (*)[101]'|
warning: passing argument 4 of 'print' from incompatible pointer type [enabled by default]|
note: expected 'char *' but argument is of type 'char (*)[101]'|


How to resolve these errors?
 
Physics news on Phys.org
  • #2
- you need to remove the & before psurname and pname in main(). In C, if you pass an array to a funcion, a pointer the the array is always passed instead.
- If you want to pass an char array to a function in C i thinks it's better to just pass a pointer to char. (or a pointer to char + a maximum length),
- the global variables psurname and pname are only used in the print function. It would be easier to make them local.
 
  • #3
while(n<1);
 
  • #4
theodoros.mihos said:
while(n<1);
What is your comment?

I don't understand the purpose of the following do ... while loop, but it will always run at least once.
C:
do
  {
     printf("n=");
     scanf("%d",&n);
  } while(n<1);
 

1. What is a structure in C?

A structure in C is a user-defined data type that allows you to group together different data types under one name. It is similar to a record in other programming languages and can hold multiple pieces of data.

2. How do you declare a structure in C?

To declare a structure in C, you use the "struct" keyword followed by the name of the structure and a set of curly braces containing the structure members. For example:
struct Person {
    char name[20];
    int age;
    float height;
};

3. How do you access structure members in C?

To access structure members in C, you use the dot (.) operator. For example, if you have a structure called "person" with a member "name", you can access it using "person.name". You can also use the arrow (->) operator when working with pointers to structures.

4. Can you have structures within structures in C?

Yes, you can have structures within structures in C. This is known as nested structures. It allows you to create more complex data structures by grouping together multiple simpler structures.

5. How do you manipulate structures in C?

To manipulate structures in C, you can use various functions and operators such as the dot (.) operator for member access, the arrow (->) operator for pointer access, the sizeof() function to get the size of a structure, and the strcpy() function to copy strings into structure members. You can also use loops and conditional statements to manipulate structures within arrays or pointers to structures.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
929
  • Engineering and Comp Sci Homework Help
Replies
3
Views
757
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
893
Back
Top