Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从函数传递和返回结构_C - Fatal编程技术网

C 从函数传递和返回结构

C 从函数传递和返回结构,c,C,我有一个带有book struct数组的函数,但当我试图将其返回到main时,它不会返回值并将其存储在数组中。如果addBook函数必须是空的,我将如何解决这个问题,以便以后可以访问数组元素 void addBook(struct Book book[], int *size) { if (*size == MAX_BOOKS) { printf("The inventory is full\n"); } else { printf(

我有一个带有book struct数组的函数,但当我试图将其返回到main时,它不会返回值并将其存储在数组中。如果addBook函数必须是空的,我将如何解决这个问题,以便以后可以访问数组元素

void addBook(struct Book book[], int *size) {

    if (*size == MAX_BOOKS) {
        printf("The inventory is full\n");

    }
    else {

        printf("ISBN:");
        scanf("%d", &book[*size]._isbn);
        printf("Title:");
        scanf("%s", book[*size]._title);
        getchar();
        printf("Year:");
        scanf("%d", &book[*size]._year);
        printf("Price:");
        scanf("%f", &book[*size]._price);
        printf("Quantity:");
        scanf("%d", &book[*size]._qty);
        *size++;
        printf("The book is successfully added to the inventory.\n");
    }
    return book;
}

int main(void) {

    struct Book book[MAX_BOOKS];
    int size = 0;
    int i;
    int option;


    printf("Welcome to the Book Store\n");
    printf("=========================\n");

    do {
        menu();
        printf("Select: ");
        scanf("%d", &option);

        switch (option) {

        case 0:
            printf("Goodbye!\n");
            break;
        case 1:
            displayInventory(book, size);
            break;
        case 2:
            addBook(book, &size);
            break;
        case 3:
            //checkPrice();
            break;
        default:
            printf("Invalid input, try again:\n");
        }
    } while (option != 0);
}

您的return语句不会执行您想要的操作,因为
addBook
的函数签名表明它返回void。我感到惊讶的是,实际上编译的代码没有关于这一点的错误

无论如何,图书数据可以以与传入数据相同的方式返回——作为输入和输出参数

基本上,您的代码可以如下所示(这只是编译代码并将标准输入的信息保存到书中的示例):


希望这能回答您的问题。

我认为您的问题在
*size++。应该使用括号,否则将修改指向大小的指针,而不是值。它应该是
(*大小)+

此外,addBook函数不应返回任何内容,因为它是空的,并且它已经在更改数组book的内容。

1)指针不是它指向的对象!2) 您的函数是
void
,它不返回任何内容。正确格式化代码!如果你得到一个警告,你有太多的代码太少的文本,这应该告诉你一些事情!不要通过不格式化代码来解决这个问题。你的讽刺也不能修复代码!为了回答您的问题,您可以通过访问
书籍[size-1]
@Olaf 1)访问上次插入的元素。我在第2点中采用了您的发言风格。我现在就开始!
#include <stdio.h>

struct Book {
    int value;
};

#define MAX_BOOKS 2

void addBook(struct Book book[], int *size) {
    if (*size == MAX_BOOKS) {
        printf("The inventory is full\n");
    }
    else {
        printf("Value:");
        scanf("%d", &book[*size].value);
        (*size)++;
        printf("The book is successfully added to the inventory.\n");
    }
}

int main(void) {
    struct Book book[MAX_BOOKS];
    int size = 0;

    addBook(book, &size);
    printf("Book 1: Value=%d\n", book[0].value);
}
$ ./main
Value:9
The book is successfully added to the inventory.
Book 1: Value=9