私は現在 c 言語を学んでいて、このコードに出くわしました。ptr はすでに変数のポインター型であるため、演算子の影響は何ですか&
。通常、演算子は非ポインター変数のアドレスを取得するために使用することがわかっています。
struct name {
int a; float b; char c[30];
};
int main()
{
struct name *ptr;
int i,n;
printf("Enter n: ");
scanf("%d",&n);
ptr = (struct name*)malloc(n*sizeof(struct name));
/* Above statement allocates the memory for n structures with pointer ptr pointing to base address */
for(i=0; i<n; ++i) {
printf("Enter string, integer and floating number respectively:\n");
scanf("%s%d%f", &(ptr+i)->c, &(ptr+i)->a, &(ptr+i)->b);
}
}