我有一个名为book的结构,它被定义为:
struct book
{
char name[30];
};
我以这种方式创建了一个结构数组:
struct book b[2];
我把它传给了一个函数,想像b[i].name
一样访问它,因为我在传递数组,所以我必须处理函数中的指针。因为我使用for循环,所以我写了ptr[i]->name
,但这不起作用。
我在传递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 bookn");
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
}
}