繁体   English   中英

访问作为结构数组传递给 function 的结构成员

[英]Accessing the member of structure passed to a function as array of structure

我有一个名为 book 的结构,其定义为:

struct book
{
char name[30];
};

我以这种方式创建了一个结构数组:

  struct book b[2];

我将它传递给 function 并希望像b[i].name一样访问它,因为我正在传递数组我必须处理 function.中的指针。因为我正在使用 for 循环我写了ptr[i]->name但是那是行不通的。

我在Passing a array of structs in C中发现了一个类似的问题,但这并不能解决我的问题。 我的总计划是:

#include<stdio.h>

 struct book
 {
   char name[30];
 };
void input(struct book [2]);
void display(struct book [2]);

 int main()
 {
 struct book b[2];
 struct book;
 input(b);
 display(b);
 }

void input(struct book *b1)
{
for (int i = 0 ; i < 2 ; i++)
{
 printf("Enter the name of book\n");
 scanf(" %[^\n]s",b1[i]->name);
}

}
void display(struct book *b1)
{
 for (int i = 0 ; i < 2 ; i++)
  {
 printf("Name of book: %s",b1[i]->name);
  }
 }

可以像下面这样访问该成员( display()input()的相同修复)

void display(struct book *b1)
{
    for (int i = 0 ; i < 2 ; i++)
    {
        printf("Name of book: %s",b1[i].name); // here
    }
 }

暂无
暂无

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

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