繁体   English   中英

在结构 function 中传递结构参数

[英]passing a struct argument in a struct function

我正在测试 struct function "push"中的 struct array 参数的传递,但收到错误消息

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

我该如何解决这个错误? 我不知道需要做什么才能使其工作。 我已经声明了结构并在 header 文件中推送了 function。

该程序的主要目的是访问书架结构书本结构的值。

这是我的代码片段。

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 文件:

#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];
}

有几个问题。 这是导致问题的第一个:

push(shelf_item[0]);

错误消息是您正在传递shelf ,而shelf *是预期的(数组)。 只需传递项目的地址(数组的开头): &shelf_item[0]

原型

shelf push(shelf item[100]);

如果您只想推送一项,则没有任何意义。

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

被宣布两次。

如果 function 只传递了一个参数但使用隐藏/全局变量也可以使用,那么这是“糟糕的风格”(TM)。

并且您的打印语句不会打印返回项目的值。

1. arguments 不匹配。

当作为参数传递的shelf item[100]将衰减到shelf *item

当您将shelf object 作为参数(数组的第一个元素)传递时,这将不起作用。


2.注意这个声明:

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

导致未定义的行为, b_title结构成员是一个未初始化的指针,就像s_title ,在它指向有效的 memory 位置之前,您不能在其中存储任何内容,或者您可以使用 char 数组:

char b_title[100];

char s_title[100];

要将字符串分配给它,您需要使用strcpy或类似的方法。


3.还有你声明book book_details[100]; shelf shelf_item[100]; 两次,在全球 scope 和再次在main


假设全局shelf_itembook_detais arrays 你可以这样做:

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文件

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;
}

假设本地shelf_itembook_detais arrays 你可以这样做:

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文件

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