簡體   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