使用指针更改结构内的数据

时间:2022-02-25 19:54:10

I created a struct that holds book info. Then created two functions: one changes the info of every type of data inside the struct, and the other that prints that info. It works fine when i enter the data but it crashes when it tries printing it, is it because the IDE gets confused when i call the functions in main()? or any other reason i'm unable to see?

我创建了一个包含书籍信息的结构。然后创建了两个函数:一个更改结构内部每种类型数据的信息,另一个打印该信息。它在我输入数据时工作正常,但在尝试打印时崩溃,是因为IDE在main()中调用函数时会感到困惑?或者我无法看到的任何其他原因?

Code:

#include <stdio.h>
#include <string.h>

struct Books
{
char  title[50];
char  author[50];
char  subject[100];
int   book_id;
};

//function declarations
void printBook( struct Books *book );
void changeBook ( struct Books *book );


int main()
{
struct Books Book1;     //declare book1 of type book
struct Books Book2;     //declare book2 of type book

//book 1 specification
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;

//book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;



//calling declared functions


changeBook ( &Book1 );
printBook ( &Book1 );

changeBook ( &Book2 );
printBook ( &Book2);

return 0;
}

// print Book data function declaration
void printBook( struct Books *book )
{
printf("Book title : %s\n", book -> title);
printf("Book author : %s\n", book -> author);
printf("Book subject : %s\n", book -> subject);
printf("Book book_id : %d\n", book -> book_id);
} 



// Change book data function declaration
void changeBook ( struct Books *book )
{
printf("Change book title info (max 50 characters)\n");
scanf("%s", book -> title);

printf("Change book author info max 50 characters\n");
scanf("%s", book -> author);

printf("Change book subject info max 100 characters\n");
scanf("%s", book -> subject);

printf("Change book_id info (only string values)\n");
scanf("%d", book -> book_id);

}

2 个解决方案

#1


3  

Change

scanf("%d", book -> book_id);

to

scanf("%d", &book -> book_id);

because scanf expects a pointer to the integer to scan.

因为scanf需要一个指向整数的指针来扫描。

You could edit the settings for compilation in your IDE. gcc warns for this type of mistake if options -Wall -Wextra are given.

您可以在IDE中编辑编译设置。如果给出选项-Wall -Wextra,gcc会警告这种类型的错误。

a.c:72:7: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
 scanf("%d", book -> book_id);

#2


2  

scanf("%d", &book->book_id);
            ^

You need to provide the pointer to an int for scanf

您需要为scanf提供指向int的指针

#1


3  

Change

scanf("%d", book -> book_id);

to

scanf("%d", &book -> book_id);

because scanf expects a pointer to the integer to scan.

因为scanf需要一个指向整数的指针来扫描。

You could edit the settings for compilation in your IDE. gcc warns for this type of mistake if options -Wall -Wextra are given.

您可以在IDE中编辑编译设置。如果给出选项-Wall -Wextra,gcc会警告这种类型的错误。

a.c:72:7: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
 scanf("%d", book -> book_id);

#2


2  

scanf("%d", &book->book_id);
            ^

You need to provide the pointer to an int for scanf

您需要为scanf提供指向int的指针