简体   繁体   English

在结构 function 中传递结构参数

[英]passing a struct argument in a struct function

I am testing passing of struct array argument in struct function "push" but getting an error message of我正在测试 struct function "push"中的 struct array 参数的传递,但收到错误消息

"passing 'shelf' (aka 'struct shelf') to the parameter of incompatible type 'shelf *' (aka 'struct shelf *');".  

How can I fix this error?我该如何解决这个错误? I don't know what needs to be done in order to make it work.我不知道需要做什么才能使其工作。 I have declared the structs and push function in the header file.我已经声明了结构并在 header 文件中推送了 function。

Main purpose of this program is to access the values of book struct in shelf struct .该程序的主要目的是访问书架结构书本结构的值。

Here are my code snippets.这是我的代码片段。

header file header 文件

#ifndef __POINTERS_H_
#define __POINTERS_H_

typedef struct book
{
    char *b_title;
    int b_pages;
}book;

typedef struct shelf
{
    char *s_title;
    int s_pages;
}shelf;

book book_details[100];
shelf shelf_item[100];

shelf push(shelf item[100]);


#endif // __POINTERS_H_

c file: c 文件:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pointers.h"

int main(void)
{
    book book_details[100];
    shelf shelf_item[100];

    book_details[0].b_title = "c++";
    book_details[0].b_pages = 200;

    push(shelf_item[0]);
    printf("Shelf Item's title is: %s\n", shelf_item[0].s_title);
    printf("Shelf Item's title is: %i\n", shelf_item[0].s_pages);

    return 0;
}

shelf push(shelf item[100])
{
    strcpy(item[0].s_title, book_details[0].b_title);
    item[0].s_pages = book_details[0].b_pages;

    return item[0];
}

There are several issues.有几个问题。 Here's the first one which causes the issue:这是导致问题的第一个:

push(shelf_item[0]);

The error message is that you are passing shelf while shelf * is expected (array).错误消息是您正在传递shelf ,而shelf *是预期的(数组)。 Simply pass the address of the item (the start of the array): &shelf_item[0]只需传递项目的地址(数组的开头): &shelf_item[0]

The prototype原型

shelf push(shelf item[100]);

does not make any sense if you just want to push one item.如果您只想推送一项,则没有任何意义。

Also

book book_details[100];
shelf shelf_item[100];

are declared twice.被宣布两次。

And it is "bad style" (TM) if a function got only one argument passed but uses a hidden/global variable to also work on.如果 function 只传递了一个参数但使用隐藏/全局变量也可以使用,那么这是“糟糕的风格”(TM)。

And your print statement does not print the values of the returned item.并且您的打印语句不会打印返回项目的值。

1. The arguments do not match. 1. arguments 不匹配。

shelf item[100] when passed as an argument will decay to shelf *item .当作为参数传递的shelf item[100]将衰减到shelf *item

As you are passing a shelf object as an argument(the first element of the array), that won't work.当您将shelf object 作为参数(数组的第一个元素)传递时,这将不起作用。


2. Note that this statement: 2.注意这个声明:

book_details[0].b_title = "c++"

Leads to undefined behavior, b_title struct member is an uninitialized pointer, as is s_title , you cannot store anything in it until it points to a valid memory location, alternatively you can use a char array:导致未定义的行为, b_title结构成员是一个未初始化的指针,就像s_title ,在它指向有效的 memory 位置之前,您不能在其中存储任何内容,或者您可以使用 char 数组:

char b_title[100];

And

char s_title[100];

To assign a string to it you then need to use strcpy or a similar method.要将字符串分配给它,您需要使用strcpy或类似的方法。


3. There is also the fact that you declare book book_details[100]; 3.还有你声明book book_details[100]; and shelf shelf_item[100];shelf shelf_item[100]; twice, at global scope and again in main .两次,在全球 scope 和再次在main


Assuming global shelf_item and book_detais arrays you can do something like:假设全局shelf_itembook_detais arrays 你可以这样做:

Header Header

typedef struct book {
    char b_title[100]; //b_title as char array, alternatively you can allocate memory
    int b_pages;
}book;

typedef struct shelf {
    char s_title[100]; //s_title as char array
    int s_pages;
}shelf;

book book_details[100];
shelf shelf_item[100];

void push(); //as shelf is global there is no need to pass it as an argument

C file C文件

int main(void) {
    strcpy(book_details[0].b_title ,"c++"); //strcpy
    book_details[0].b_pages = 200;

    push();

    printf("Shelf Item's title is: %s\n", shelf_item[0].s_title);
    printf("Shelf Item's title is: %i\n", shelf_item[0].s_pages);
    return 0;
}

void push() {
    strcpy(shelf_item[0].s_title, book_details[0].b_title);
    shelf_item[0].s_pages = book_details[0].b_pages;
}

Assuming local shelf_item and book_detais arrays you can do something like:假设本地shelf_itembook_detais arrays 你可以这样做:

Header Header

#define SIZE 100

typedef struct book {
    char *b_title;
    int b_pages;
}book;

typedef struct shelf {
    char *s_title;
    int s_pages;
}shelf;

void push(book *b, shelf *s); //pass both arrays as arguments

C file C文件

int main(void) {

    book book_details[SIZE];
    shelf shelf_item[SIZE];

    book_details[0].b_title = malloc(SIZE); //allocate memory for b_title
 
    if(book_details[0].b_title == NULL){ /*deal with error*/}

    strcpy(book_details[0].b_title ,"c++");
    book_details[0].b_pages = 200;

    push(book_details, shelf_item);

    printf("Shelf Item's title is: %s\n", shelf_item[0].s_title);
    printf("Shelf Item's title is: %i\n", shelf_item[0].s_pages);
    return 0;
}

void push(book *b, shelf *s) {

    s[0].s_title =  malloc(SIZE); //allocate memory for s_title
    if(s[0].s_title == NULL){ /*deal with error*/}

    strcpy(s[0].s_title, b[0].b_title);
    s[0].s_pages = b[0].b_pages;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM