[英]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.