简体   繁体   中英

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

"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.

Main purpose of this program is to access the values of book struct in shelf struct .

Here are my code snippets.

header file

#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:

#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). Simply pass the address of the item (the start of the array): &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.

And your print statement does not print the values of the returned item.

1. The arguments do not match.

shelf item[100] when passed as an argument will decay to shelf *item .

As you are passing a shelf object as an argument(the first element of the array), that won't work.


2. Note that this statement:

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:

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.


3. There is also the fact that you declare book book_details[100]; and shelf shelf_item[100]; twice, at global scope and again in main .


Assuming global shelf_item and book_detais arrays you can do something like:

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

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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