将结构传递给 C 中的函数
Passing structures to functions in C
Define a struct named Book
. Each book should have a name with
exactly 3 letters (abbreviation). Each book should also have a page
count (integer), and a price (integer).
Write a program which reads an integer n
first, then reads the
names
, page counts
and prices
of n
books.
Write a function which takes an array of books, and sorts them
according to their prices. Using that function, your program should
print the names and page counts of each book with the order of their
prices.
我的问题
有人可以向我解释如何将结构传递给函数并让这段代码工作吗?或者他们将如何解决这个问题。
struct Book{
char name[3];
int pagec;
int price;
};
void price(int size, struct Book books[size]){
int i,j, tmp;
for(i=0; i<size; i++){
for(j=0; j<size-1; j++){
if(books[j].price < books[j+1].price){
books[j].price = tmp;
books[j].price = books.price[j+1];
books.price[j+1] = tmp;
}
}
}
}
int main(void) {
int n;
scanf("%d", &n);
struct Book books[n];
int i,j;
for(i=0; i<n; i++){
for(j=0; i<1; j++){
scanf("%c", &books[i].name);
scanf("%d", &books[i].pagec);
scanf("%d", &books[i].price);
}
}
price(n, books[n]);
for(i=0; i<n; i++){
printf("%c: %d - %d",books[i].name, books[i].pagec, books[i].price);
}
所以你有你的结构
struct Book{
char name[10];
int pagec;
int price;
};
您可以通过使用 "address of" 运算符
传递指向它的指针来传递它
void receivingFunction(Book* myBook)
{
printf("%s", myBook->name);
}
void sendingFunction()
{
Book myBook;
//set values in myBook
receivingFunction(&myBook);
}
请注意,当您使用指向 book 的指针时,您使用 ->
运算符访问成员,而不是 .
运算符。
现在上面的例子只是为了传入一个实例。如果要传入数组怎么办?它看起来像这样。
#include <stdio.h>
#include <string.h>
struct Book{
char name[4]; //other answers explain well why I changed this to 4
int pagec;
int price;
};
void BookSorter(struct Book books[10], int booksLength)
{
int i;
for(i = 0; i < booksLength; i++)
{
printf("%s %d %d\n", books[i].name, books[i].pagec, books[i].price);
}
}
int main(void)
{
Book books[10];
//define your values for books here
//mine are junk values since this is just an example
for(int i = 0; i < 10; i++)
{
strncpy(books[i].name, "aaa", 4);
books[i].pagec = 4;
books[i].price = 10;
}
//
BookSorter(books, 10);
}
从这里您可以修改冒泡排序以遍历数组实例并交换它们。我不打算包括那部分,因为 1) 它超出了您关于传递结构的原始问题的范围,并且 2) 看起来您确实在做作业,我不想给您全部。您的问题下方的评论解决了解决冒泡排序的方法,我还没有看到的是您只是交换书籍的价格,而不是书籍本身。您的临时变量应该是 struct Book
,而不是 int
。您的交换只是交换书籍的价格,这将导致它们被分配给错误的书籍以最终打印出答案,书籍和页数将按照阅读时的相同顺序保留。看起来你使用了这个示例代码(或者可能是一百万个类似的代码),但如果你需要 reference for implementing bubble sort.
books[n]
是 one struct Book
(或者,如果数组有 n+1
个元素,则将是)。
数组的名称就是 books
,这是您应该传递给函数的内容:
price(n, books);
你的问题中关于如何通过 struct
的部分似乎已经得到很好的回答,让我开始第二部分:我将如何解决这个问题。
struct应该是什么描述的很好,所以我们可以不假思索地写:
/*
Define a struct named: Book.
Each book should have a name with exactly 3 letters (abbreviation).
Each book should also have a page count (integer),
and a price (integer).
*/
typedef struct book {
// three characters plus '[=10=]'
char name[4];
int pagec;
int price;
} book_t;
(不需要 typedef
,您可以省略该部分并直接将结构与 struct books
一起使用)
他们想要三个功能,一个是main()
我们可以把它作为三个功能之一来使用。我认为获取信息、分配内存等是 main()
函数的一个很好的用途。
/*
Write a program which
reads an integer n first,
then reads the names,
page counts
and prices of n books.
*/
int main()
{
int n, i;
book_t **books;
puts("How many books?");
scanf("%d", &n);
// we need enough memory to safe "n" structs
// at first allocate memory for n pointers
books = malloc(n * sizeof(book_t *));
// at each pointer allocate enough memory for one struct books
for (i = 0; i < n; i++) {
books[i] = malloc(sizeof(book_t));
}
// red the info from stdin
for (i = 0; i < n; i++) {
printf("Name of book (3 letter abbrev.):");
scanf("%3s", books[i]->name);
printf("Number of pages:");
scanf("%d", &books[i]->pagec);
printf("Price of book (incl. taxes):");
scanf("%d", &books[i]->price);
}
// call function to sort them
sort_books(books, n);
// call a function to print the sorted list
print_books(books, n);
// we don't need the memory anymore, so free it
// at first free the individual structs
for (i = 0; i < n; i++) {
free(books[i]);
}
// then free the memory holding all of the pointers
free(books);
exit(EXIT_SUCCESS);
}
打印和排序这两个函数在参数处理上是相似的
/*
Write a function which takes an array of books,
and sorts them according to their prices.
Doesn't say something about output, so sort in situ
*/
void sort_books(book_t ** books, int length)
{
// OP had bubble sort, so lets do a bubble sort. Why not?
int i, j;
book_t *tmp;
for (i = 0; i < length - 1; i++) {
for (j = 0; j < length -i - 1; j++) {
if (books[j]->price < books[j + 1]->price) {
tmp = books[j];
books[j] = books[j + 1];
books[j + 1] = tmp;
}
}
}
}
全部打印出来非常简单
/*
Using that function, your program should
print the names and page counts of each book with the order of their prices.
*/
void print_books(book_t ** books, int length)
{
int i;
for (i = 0; i < length; i++) {
printf("Name %s, pages %d, price %d\n",
books[i]->name, books[i]->pagec, books[i]->price);
}
}
它没有说明确切的顺序,我随意使用降序(从高到低)。如果你想要一个升序(从最低到最高)改变排序算法中的比较:
void sort_books(book_t ** books, int length)
{
// OP had bubble sort, so lets do a bubble sort
int i, j;
book_t *tmp;
for (i = 0; i < length - 1; i++) {
for (j = 0; j < length - i - 1; j++) {
// if (books[j]->price < books[j + 1]->price) {
if (books[j]->price > books[j + 1]->price) {
tmp = books[j];
books[j] = books[j + 1];
books[j + 1] = tmp;
}
}
}
}
请注意我省略了 所有 检查!你需要检查malloc()
和scanf()
的returns,如果n
是一个整数,如果其他数字都是数字等等!
Define a struct named
Book
. Each book should have a name with exactly 3 letters (abbreviation). Each book should also have a page count (integer), and a price (integer).Write a program which reads an integer
n
first, then reads thenames
,page counts
andprices
ofn
books.Write a function which takes an array of books, and sorts them according to their prices. Using that function, your program should print the names and page counts of each book with the order of their prices.
我的问题
有人可以向我解释如何将结构传递给函数并让这段代码工作吗?或者他们将如何解决这个问题。
struct Book{
char name[3];
int pagec;
int price;
};
void price(int size, struct Book books[size]){
int i,j, tmp;
for(i=0; i<size; i++){
for(j=0; j<size-1; j++){
if(books[j].price < books[j+1].price){
books[j].price = tmp;
books[j].price = books.price[j+1];
books.price[j+1] = tmp;
}
}
}
}
int main(void) {
int n;
scanf("%d", &n);
struct Book books[n];
int i,j;
for(i=0; i<n; i++){
for(j=0; i<1; j++){
scanf("%c", &books[i].name);
scanf("%d", &books[i].pagec);
scanf("%d", &books[i].price);
}
}
price(n, books[n]);
for(i=0; i<n; i++){
printf("%c: %d - %d",books[i].name, books[i].pagec, books[i].price);
}
所以你有你的结构
struct Book{
char name[10];
int pagec;
int price;
};
您可以通过使用 "address of" 运算符
传递指向它的指针来传递它void receivingFunction(Book* myBook)
{
printf("%s", myBook->name);
}
void sendingFunction()
{
Book myBook;
//set values in myBook
receivingFunction(&myBook);
}
请注意,当您使用指向 book 的指针时,您使用 ->
运算符访问成员,而不是 .
运算符。
现在上面的例子只是为了传入一个实例。如果要传入数组怎么办?它看起来像这样。
#include <stdio.h>
#include <string.h>
struct Book{
char name[4]; //other answers explain well why I changed this to 4
int pagec;
int price;
};
void BookSorter(struct Book books[10], int booksLength)
{
int i;
for(i = 0; i < booksLength; i++)
{
printf("%s %d %d\n", books[i].name, books[i].pagec, books[i].price);
}
}
int main(void)
{
Book books[10];
//define your values for books here
//mine are junk values since this is just an example
for(int i = 0; i < 10; i++)
{
strncpy(books[i].name, "aaa", 4);
books[i].pagec = 4;
books[i].price = 10;
}
//
BookSorter(books, 10);
}
从这里您可以修改冒泡排序以遍历数组实例并交换它们。我不打算包括那部分,因为 1) 它超出了您关于传递结构的原始问题的范围,并且 2) 看起来您确实在做作业,我不想给您全部。您的问题下方的评论解决了解决冒泡排序的方法,我还没有看到的是您只是交换书籍的价格,而不是书籍本身。您的临时变量应该是 struct Book
,而不是 int
。您的交换只是交换书籍的价格,这将导致它们被分配给错误的书籍以最终打印出答案,书籍和页数将按照阅读时的相同顺序保留。看起来你使用了这个示例代码(或者可能是一百万个类似的代码),但如果你需要 reference for implementing bubble sort.
books[n]
是 one struct Book
(或者,如果数组有 n+1
个元素,则将是)。
数组的名称就是 books
,这是您应该传递给函数的内容:
price(n, books);
你的问题中关于如何通过 struct
的部分似乎已经得到很好的回答,让我开始第二部分:我将如何解决这个问题。
struct应该是什么描述的很好,所以我们可以不假思索地写:
/*
Define a struct named: Book.
Each book should have a name with exactly 3 letters (abbreviation).
Each book should also have a page count (integer),
and a price (integer).
*/
typedef struct book {
// three characters plus '[=10=]'
char name[4];
int pagec;
int price;
} book_t;
(不需要 typedef
,您可以省略该部分并直接将结构与 struct books
一起使用)
他们想要三个功能,一个是main()
我们可以把它作为三个功能之一来使用。我认为获取信息、分配内存等是 main()
函数的一个很好的用途。
/*
Write a program which
reads an integer n first,
then reads the names,
page counts
and prices of n books.
*/
int main()
{
int n, i;
book_t **books;
puts("How many books?");
scanf("%d", &n);
// we need enough memory to safe "n" structs
// at first allocate memory for n pointers
books = malloc(n * sizeof(book_t *));
// at each pointer allocate enough memory for one struct books
for (i = 0; i < n; i++) {
books[i] = malloc(sizeof(book_t));
}
// red the info from stdin
for (i = 0; i < n; i++) {
printf("Name of book (3 letter abbrev.):");
scanf("%3s", books[i]->name);
printf("Number of pages:");
scanf("%d", &books[i]->pagec);
printf("Price of book (incl. taxes):");
scanf("%d", &books[i]->price);
}
// call function to sort them
sort_books(books, n);
// call a function to print the sorted list
print_books(books, n);
// we don't need the memory anymore, so free it
// at first free the individual structs
for (i = 0; i < n; i++) {
free(books[i]);
}
// then free the memory holding all of the pointers
free(books);
exit(EXIT_SUCCESS);
}
打印和排序这两个函数在参数处理上是相似的
/*
Write a function which takes an array of books,
and sorts them according to their prices.
Doesn't say something about output, so sort in situ
*/
void sort_books(book_t ** books, int length)
{
// OP had bubble sort, so lets do a bubble sort. Why not?
int i, j;
book_t *tmp;
for (i = 0; i < length - 1; i++) {
for (j = 0; j < length -i - 1; j++) {
if (books[j]->price < books[j + 1]->price) {
tmp = books[j];
books[j] = books[j + 1];
books[j + 1] = tmp;
}
}
}
}
全部打印出来非常简单
/*
Using that function, your program should
print the names and page counts of each book with the order of their prices.
*/
void print_books(book_t ** books, int length)
{
int i;
for (i = 0; i < length; i++) {
printf("Name %s, pages %d, price %d\n",
books[i]->name, books[i]->pagec, books[i]->price);
}
}
它没有说明确切的顺序,我随意使用降序(从高到低)。如果你想要一个升序(从最低到最高)改变排序算法中的比较:
void sort_books(book_t ** books, int length)
{
// OP had bubble sort, so lets do a bubble sort
int i, j;
book_t *tmp;
for (i = 0; i < length - 1; i++) {
for (j = 0; j < length - i - 1; j++) {
// if (books[j]->price < books[j + 1]->price) {
if (books[j]->price > books[j + 1]->price) {
tmp = books[j];
books[j] = books[j + 1];
books[j + 1] = tmp;
}
}
}
}
请注意我省略了 所有 检查!你需要检查malloc()
和scanf()
的returns,如果n
是一个整数,如果其他数字都是数字等等!